get imageurl from picture library linked to lookup column in list
-
07-10-2020 - |
Question
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; }
}
Solution
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();
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange