0

I am trying to upload a picture to a Picture column in a custom sharepoint list programatically.I am ok with using either client or server object models. All the examples I've seen so far only address uploading files to document/picture libraries.In my case I need to do it in a custom list. Any ideas how to go about doing this? Thanks

1

just to make it clear, you cant directly upload the image to the list! well thats what i know off!,

you need two steps...

1) upload the image to an image library

2) update the list with the image url

step one can be done through code by:

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;

https://stackoverflow.com/questions/13602717/cannot-upload-image-to-a-sharepoint-list

or

FieldUrlValue _url = new FieldUrlValue();
_url.Url = txtImgUrl.Text;
_url.Description = txtImgTitle.Text;
listItem["Image"] = _url;

client side you could do somthing like:

        ClientContext context = new ClientContext(Url);
        List list = context.Web.Lists.GetByTitle(ImageList);
        CamlQuery query = new CamlQuery();
        query.ViewXml = "<View/>";

        ListItemCollection listItems = list.GetItems(query);

        context.Load(listItems);

        context.ExecuteQuery();

        ListItem item = listItems[listItems.Count+1];

        item["Title"] = txtTitle.Text;

        ((Microsoft.SharePoint.Client.FieldUrlValue)(item["ImageUrl"])).Url = txtImgUrl.Text;
        ((Microsoft.SharePoint.Client.FieldUrlValue)(item["ImageUrl"])).Description = txtImgTitle.Text;

        item.Update();
        context.ExecuteQuery();

call the list as usual, difference is here! the list item column is a "hyperlink or picture", in this case its a picture that takes in a url of the image and text for description! what i did in client side can easily be repeated! it becomes:

get the list as usual (call spsite then spweb then splist), add list item as usual but by doing it thisway! SPFieldUrlValue is the type and item is SPListItem from SPList :).

((SPFieldUrlValue)(item["ImageUrl"])).Url = path;

((SPFieldUrlValue)(item["ImageUrl"])).Description = description;

the varible path and description is whatever or whereever your getting the values from!

as you can see there are a few ways! I like the last way as its easy to understand and doesnt take long to code(less space!), dont forget to update the list and spweb!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.