Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot upload Image to a SharePoint List

I'm working with a Visual WebPart and I want to upload a image from my FileUpload control to a SharePoint list. This is the code I'm using but I can't get it to work, (Title, prodnum, color, etc is working, but not image). I've also installed SparQube.

This is my ListView: Image .

protected void Button_Save_Click(object sender, EventArgs e)
{
    SPSite currentSite = SPContext.Current.Site;
    SPList myList = currentSite.RootWeb.Lists.TryGetList("SharePointDatabase");

    try
    {
        if (myList != null && FileUpload_Pic.PostedFile != null && FileUpload_Pic.HasFile)
        {
            SPListItem listItem = myList.Items.Add();

            listItem["Title"] = TextBox_Name.Text;
            listItem["ProductNumber"] = TextBox_ProdNum.Text;
            listItem["Color"] = TextBox_Color.Text;
            listItem["ListPrice"] = TextBox_ListPrice.Text;
            listItem["MoreInformation"] = TextBox_MoreInfo.Text;

            string fileName = Path.GetFileName(FileUpload_Pic.PostedFile.FileName);
            listItem["Image"] = fileName;

            listItem.Update();                                            

            TextBox_Search.Text = string.Empty;
            TextBox_Name.Text = string.Empty;
            TextBox_MoreInfo.Text = string.Empty;
            TextBox_ProdNum.Text = string.Empty;
            TextBox_Color.Text = string.Empty;
            TextBox_ListPrice.Text = string.Empty;

            Label_Exception.Text = "Saved to Database list.";

            Dispose();
        }
    }
    catch (Exception x)
    {
        Label_Exception.Text = x.Message;
    }

}
like image 807
krisal Avatar asked Nov 28 '12 10:11

krisal


People also ask

How do I add an image to a SharePoint list?

Click Edit in the page, if you haven't already done so. Click on the Insert tab in the ribbon and then click on Insert Image. You are now in your SharePoint Images library and can select the image you want to insert by highlighting it. Click OK.

Why can't I upload file to SharePoint?

To create and upload files and folders in a library, you must have contributor permissions to the library. If you're not sure what permissions you have, contact your SharePoint Admin. For more information, see Types of files that cannot be added to a list or library.

Why do I keep getting upload failed on SharePoint?

The fix for this error is relatively simple once one knows the cause. We need to clear out the cached documents in Microsoft Upload Center, and then tell it to always delete files from the cache when they are closed. In Windows 10: Type Upload in the Cortana search bar (bottom left, right beside the start menu button)


1 Answers

You can add file as stream directly from File Upload to specific web using following method and then add the file path to the list as shown in below example,

SPContext.Current.Web.Files.Add(String.Concat(SPContext.Current.Web.Site.RootWeb.Url, path), stream, true);

path is relative path for the image. in user case file name. stream can get using FileUpload.FileContent in file upload control

Then add this path to the list as below.

listItem["Image"] = path;

This is work for all the browsers.

like image 144
Telan Niranga Avatar answered Oct 11 '22 22:10

Telan Niranga