1

I have list called "Employee" and it has lookup column called as "City". This lookup column is Connected to Picture libraray. I wish to retrieve imageurl from it. How to achieve it. I tried this so far but unable to get.

SPList list = spWeb.Lists["Employee"];
SPList picLibCity = spWeb.Lists["City"];

foreach (SPListItem item in items)
{
   EmpList.Add(new EmpLinks
   {
       Title = item["Title"].ToString(),
       City= new SPFieldLookupValue(item["City"] as String).LookupValue
   });
}

Then i am trying following to get imageurl

foreach (var row in EmpList)
{
    if (row.City!= null)
    {
        string group = row.city;
        SPItem lookupItem = picLibCity.GetItemById(group.LookupId);
        string imageUrl = lookupItem["ImageURL"].ToString();
    }

 }

This is my EmpLinks Class

 internal class EmpLinks
 {
        public string Title { set; get; }
        public string City{ set; get; }

 }

1 Answer 1

3

Instead of SPList you should use SPPictureLibrary. I mean

SPList picLibCity = (SPPictureLibrary) spWeb.Lists["City"];

And instead of ImageURL, you should use ServerUrl. I mean

foreach (var row in EmpList)
{
    if (row.City!= null)
    {
        string group = row.city;
        SPItem lookupItem = picLibCity.GetItemById(group.LookupId);
        string imageUrl = lookupItem["ServerUrl"].ToString();
    }

 }

Full modified code

SPList list = spWeb.Lists["Employee"];
SPList picLibCity = (SPPictureLibrary) spWeb.Lists["City"];

foreach (SPListItem item in items)
{
   EmpList.Add(new EmpLinks
   {
       Title = item["Title"].ToString(),
       City= new SPFieldLookupValue(item["City"] as String)
   });
}

foreach (var row in EmpList)
    {
        if (row.City!= null)
        {
            SPItem lookupItem = picLibCity.GetItemById(row.City.LookupId);
            string imageUrl = lookupItem["ServerUrl"].ToString();
        }

     }

internal class EmpLinks
 {
        public string Title { set; get; }
        public SPFieldLookupValue City{ set; get; }

 }

If you need absolute URL, then you can use EncodedAbsUrl. I mean

string imageUrl = lookupItem["EncodedAbsUrl"].ToString();
5
  • i cant get Group.lookupId as Group is string
    – Happy
    Commented Feb 7, 2016 at 16:44
  • Try as per my modified code Commented Feb 7, 2016 at 16:50
  • I am trying now
    – Happy
    Commented Feb 7, 2016 at 16:52
  • i get url as /sites/mystie/City/myimg.PNG... how can i show this in image.. do i need to just retrieve server name and add this url or any other method
    – Happy
    Commented Feb 7, 2016 at 16:57
  • No. just use string imageUrl = lookupItem["EncodedAbsUrl"].ToString();. I have updated the answer. Commented Feb 7, 2016 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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