Logo 
Search:

Sharepoint 2010 Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint 2010       RSS Feeds

Display Publishing Image to the page from a SPList

  Asked By: Shradha    Date: Aug 05    Category: Sharepoint 2010    Views: 8180

I have a custom list with two fields.
Title that holds integer value and Image(that holds the Url of the image.

In my pagelayout.aspx page, I have an event Page_load. Inside it, I want to display the images from that SPList randomly on each page refresh.

if (pubPage != null && pubPage.Layout.Title == "ABCHome Page Layout")
{

//Get the list
SPList HomepageImage = currentWeb.Lists["Homepage Images"];

//Get the list items
SPListItemCollection collListItems = HomepageImage.Items;


//Select one of the list items
Random random = new Random();
//bannerImage = string.Format("<img src=\"{0}/{1}\">", HomepageImage.Items[random.Next(0, collListitems.Count - 1)].Url);


break;
}

//testImage is the Image control defined inside Placeholder Main.
testImage.ImageUrl = bannerImage;

--- Its not working. Can you please let me know a proper way to complete my task?

Thanks.

Share: 

 

6 Answers Found

 
Answer #1    Answered By: Jordon Willis     Answered On: Aug 05

Are you able to get Image Path correctly? I have a feeling you are not getting Image Path correctly. I have made minor change to your code. Let me know if that works for you. Incase it didn't let me know at what point it is failing.


if (pubPage != null  && pubPage.Layout.Title == "ABCHome page  Layout")
{

//Get the list
SPList HomepageImage = currentWeb.Lists["Homepage Images"];

//Get the list  items
SPListItemCollection collListItems = HomepageImage.Items;

string strBannerImage = HomepageImage.Items[random.Next(0, collListitems.Count - 1)].Url;


//Select one of the list items  
Random random  = new Random();
bannerImage = "<img src='" + strBannerImage + "'>";

break;
}

 
Answer #2    Answered By: Shradha Shrestha     Answered On: Aug 06

Thanks for replying to my post. I got that working. :)

 
Answer #3    Answered By: Jordon Willis     Answered On: Aug 06

Did my solution work or you tried something else?

Could you please provide the solution so it would be helpful to everybody.

 
Answer #4    Answered By: Shradha Shrestha     Answered On: Aug 13

Jordon,

This is how I did:
if (pubPage != null  && pubPage.Layout.Title == "Home page  Layout")
{

//Get the list
SPList HomepageImage = currentWeb.Lists["Homepage Images"];

//Create a query
SPQuery query = new SPQuery();
query.Query = string.Format("<Where><And><Gt><FieldRef Name='ID' /><Value Type='Counter'>0</Value></Gt><Leq><FieldRef Name='ID' /><Value Type='Counter'>4</Value></Leq></And></Where>");

//Get the list  items
SPListItemCollection collListItems = HomepageImage.GetItems(query);

//Select one of the list items randomly  
 random  random = new Random();
int i = random.Next(1, 4);
 string  sUrl = "";

foreach (SPListItem Listitem in collListItems)
{

if (Listitem["ID"].ToString().CompareTo(i.ToString()) == 0)
{


Microsoft.SharePoint.Publishing.Fields.ImageFieldValue h = (Microsoft.SharePoint.Publishing.Fields.ImageFieldValue)Listitem["Image"];
testImage.ImageUrl = h.ImageUrl;

break;
}


testImage.ImageUrl = sUrl;

}

}

 
Answer #5    Answered By: Shradha Shrestha     Answered On: Aug 13


0

I have a SPList which has three fields: image  that holds Url of the publishing  image and ImageDescription that holds a single line of text for description and a checkbox field called ImageChecked(yes/NO)

The probelm is if the checkbox is selected, it should display  the image and the ImageDescription on the aspx  page. Ideally there is only one item in the list  with checkbox yes.

I m using SPQuery to query the list.

Here's how I did it. However its not giving me the result I wanted. I created a usercontrol1.ascx that has



//usercontrol1.ascx.cs has got the code below in page  load event.
if (currentWeb != null  && currentWeb.Site.RootWeb != null)
{

if (pubPage != null && pubPage.Layout.Title == "Test Home Page Layout")
{

//Get the list
SPList FeaturedImage = currentWeb.Lists["FeaturedImageDetails"];

//Create a query
SPQuery query = new SPQuery();
query.Query = string.Format("<Where><Eq><FieldRef Name='ImageFeaturedHD' /<Value Type='Boolean'>1</Value></Eq></Where>");


//Get the list items
SPListItemCollection collListItems = FeaturedImage.GetItems(query);
 string  sUrl = "";
foreach (SPListItem Listitem in collListItems)
{
Microsoft.SharePoint.Publishing.Fields.ImageFieldValue h = (Microsoft.SharePoint.Publishing.Fields.ImageFieldValue)Listitem["Image"];

testImage.ImageUrl = h.ImageUrl;
testLabel.Text = Listitem["ImageDescHD"].ToString();


break;

}



testImage.ImageUrl = sUrl;


}

}
}

Any thoughts on how to achieve this? I am not getting image nor description of image for a checked image. Please help me with this problem.
Thanks.

 
Didn't find what you were looking for? Find more on Display Publishing Image to the page from a SPList Or get search suggestion and latest updates.




Tagged: