Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am tryin to retrieve the url of image attachments from a OOTB announcements list and add it to a datatable column I created named "Images". The respective image control in my web part displays the same image as the first announcement on all announcements. How do I increment the row so that the next url is taken?

public partial class AnnouncementTrial2UserControl : UserControl
{
    private string imageUrl="";
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            SPWeb web = SPContext.Current.Web;
            web.AllowUnsafeUpdates = true;
            SPList list = web.Lists["Announcements"];

            SPQuery spQuery = new SPQuery();
            spQuery.Query = " <Where><And><IsNotNull><FieldRef Name='Title' /></IsNotNull><Or><IsNull><FieldRef Name='Expires' /></IsNull><Geq><FieldRef Name='Expires' /><Value Type='Date'><Today /></Value></Geq></Or></And></Where><OrderBy><FieldRef Name='Expires' Ascending='True' /></OrderBy>";
            spQuery.RowLimit = 5;
            SPListItemCollection oListCollection = list.GetItems(spQuery);

            System.Data.DataTable table = new System.Data.DataTable();
            table = oListCollection.GetDataTable();
            table.Columns.Add("Images", typeof(String));
            int rowix = 0;
            foreach (SPListItem item in oListCollection)
            {
                DataRow dr = table.Rows[rowix++];
                if (item.Attachments.Count > 0)
                {
                    imageUrl = SPUrlUtility.CombineUrl(item.Attachments.UrlPrefix, item.Attachments[0]);
                    dr["Images"] = imageUrl;
                }
            }
            repAnnouncements.ItemDataBound += new RepeaterItemEventHandler(repAnnouncements_ItemDataBound);
            repAnnouncements.DataSource = table;
            repAnnouncements.DataBind();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    void repAnnouncements_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {                
        try
        {
            //Setting the imageUrl for the actual IMage
            ((Image)e.Item.FindControl("imgLink")).ImageUrl = imageUrl;

            //Setting the .NavigateUrl property fot the Title
            ((HyperLink)e.Item.FindControl("hypTextEditLink")).NavigateUrl = SPContext.Current.Site.RootWeb.Url + "/Lists/Announcements/DispForm.aspx?ID=" + ((HiddenField)e.Item.FindControl("ItemID")).Value;
            //Setting the .NavigateUrl property fot the Image
            ((HyperLink)e.Item.FindControl("hypImageEditLink")).NavigateUrl = SPContext.Current.Site.RootWeb.Url + "/Lists/Announcements/DispForm.aspx?ID=" + ((HiddenField)e.Item.FindControl("ItemID")).Value;
        }
        catch (Exception ex)
        { throw ex; }
    }
}
share|improve this question
add comment

2 Answers

For each row in the data table you're looping through all the listitems end up having updated the "Images" cell with all images leaving the last in place.

You could do something like:

System.Data.DataTable table = new System.Data.DataTable(); 
table = oListCollection.GetDataTable(); 
table.Columns.Add("Images", typeof(String));
int rowix = 0;
foreach (SPListItem item in oListCollection) 
{ 
    DataRow dr = table.Rows[rowix++];
    if (item.Attachments.Count > 0) 
    { 
        imageUrl = SPUrlUtility.CombineUrl(item.Attachments.UrlPrefix, item.Attachments[0]); 
        dr["Images"] = imageUrl; 
     } 
}
share|improve this answer
    
I have edited my post to include the entire code. I am using your code (from a previous post)to retrieve url for the images but am trying to populate an asp:image control within an item template of a repeater control. It still displays jus tthe one image on all announcements –  user7400 Mar 30 '12 at 17:13
add comment
up vote 0 down vote accepted

This worked for me:

            SPQuery spQuery = new SPQuery();
            spQuery.Query = " <Where><And><IsNotNull><FieldRef Name='Title' /></IsNotNull><Or><IsNull><FieldRef Name='Expires' /></IsNull><Geq><FieldRef Name='Expires' /><Value Type='Date'><Today /></Value></Geq></Or></And></Where><OrderBy><FieldRef Name='Expires' Ascending='True' /></OrderBy>";
            spQuery.RowLimit = 4;
            SPListItemCollection oListCollection = list.GetItems(spQuery);
            //This is to check and see if the list contains any items at all!   
            if (oListCollection.Count > 0)
            {
                System.Data.DataTable table = new System.Data.DataTable();
                table = oListCollection.GetDataTable();
                table.Columns.Add("Images", typeof(String));

                foreach (DataRow DR in table.Rows)
                {
                    int ID = Convert.ToInt32(DR["ID"]);
                    SPListItem ITEM = list.GetItemById(ID);
                    if (ITEM.Attachments.Count > 0)
                    {
                        imageUrl = SPUrlUtility.CombineUrl(ITEM.Attachments.UrlPrefix, ITEM.Attachments[0]);
                        DR["Images"] = imageUrl;
                    }
                    else
                    {
                        //This sets the image to a default image if no attachment exists!
                        DR["Images"] = "/_Layouts/AnnouncementPic.png";
                    }
                }

AND just specify

 <asp:Image ID="imgLink" runat="server" Width="150px" Height="110px" ImageUrl='<%#Eval("Images")%>'/>

instead of calling it in the event repeater handler.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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