I'm trying to retrieve image/attachment url from custom list, while I am fetching image url and passing to img tag then it returns true rather Image URL,like src="true" and i want it like src="http://abc.png"

    private const string TitleColumn = "Title";        
    private const string Attchment = "Attachments";
    var webUrl = SPContext.Current.Web.Url;

    using (var site = new SPSite(webUrl))
        using (var web = site.OpenWeb())
                var List = web.Lists["SimpleList"];
                var spQuery = new SPQuery
                    Query = "</Query><ViewFields><FieldRef Name=" + TitleColumn + "/><FieldRef Name=" + Attchment + "/>"

                var Items = List.GetItems(spQuery);
                if (Items != null && Items.Count > 0)
                    foreach (SPListItem item in Items)
                        var ImageLink = new SPFieldUrlValue(item[Attchment].ToString());
                        imageTag.Attributes.Add("src", ImageLink.Url);                        
                        imageDiv.Controls.Add(imageTag);//image added to image div
            catch (Exception ex)  {   }                  

This approach works SSOM. How can I get the image url in sandbox solution SharePoint online CSOM?

  • It should work in the sandboxed solution as well! What's your field name? if it is Attchment, then put it like new SPFieldUrlValue(item["Attchment"].ToString()); – Atish Dipongkor - MVP Jun 16 '16 at 11:54
  • i have edited the question – sharepoint2013 Jun 16 '16 at 12:10

Try below method as detailed by skeletank:

private IEnumerable<string> GetAttachmentUrls(SPListItem item)
  return from string fileName in item.Attachments
         orderby fileName
         select SPUrlUtility.CombineUrl(item.Attachments.UrlPrefix, fileName);
  • how do i implement this with the above mensioned approch?means m not undesrstnding,how to pass that specific value to this function. – sharepoint2013 Jun 16 '16 at 12:42
  • As per my understand, You have attached images to list item so Pass the list item object in this method and it will return the attachment urls. – Dikesh Gandhi Jun 16 '16 at 12:46
  • You can change return type of this method to string array as per your need – Dikesh Gandhi Jun 16 '16 at 12:47
  • like this? GetAttachmentUrls(item); – sharepoint2013 Jun 16 '16 at 12:53
  • 1
    Can you give the reference from where you copied? I think it is: sharepoint.stackexchange.com/a/32080/21421 @DikeshGandhi – Atish Dipongkor - MVP Jun 16 '16 at 13:09

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.