Question

Please bear with me as I am brand new to the whole sharepoint world. I want to be able to display images that have been attached to OOTB announcement list in a custom visual web part I am trying to build. I would also like to be able to show the title (from the list) right under the image as a hyperlink (I am thinking I would have to convert it after retrieving it from the list) I want to display three images in a row with some spacing in between them. Here is the code I have as of now:

using (SPSite oSPsite = new SPSite("http://sharepointdev:2000/"))
        {

            using (SPWeb oSPWeb = oSPsite.OpenWeb())
            {
                oSPWeb.AllowUnsafeUpdates = true;

                // Fetch the List
                SPList list = oSPWeb.Lists["Announcements"];

                SPQuery spQuery = new SPQuery();
               spQuery.Query = "<Where> <Eq> <FieldRef Name='Title' /> <Value Type='Text'></Value> </Eq> </Where>";
                spQuery.Query = "";
                spQuery.RowLimit = 3;

                // Show item in text box
                SPListItemCollection oListCollection = list.GetItems(spQuery);
                foreach (SPListItem item in oListCollection)
                {

                    ListBox1.Items.Add(item.Title);

                }


            }

enter image description here

Was it helpful?

Solution

First a number of small remarks:

  • Usually you don't hardcode the URL of the site you want to work against. It may be ok but if you want to access the current site the just use SPContext.Current.Web
  • Unless you need to make updates on a get you shouldn't set AllowUnsafeUpdates
  • A listbox will not allow you to add images inside it (so in my code I just added a table with links)

To get the full url of attachment you use item.Attachment.UrlPrefix and then add the filename of each attachment. So a sample might be like this:

protected override void CreateChildControls()
{
    base.CreateChildControls();
    SPWeb oSPWeb = SPContext.Current.Web;

    // Fetch the List  
    SPList list = oSPWeb.Lists["Announcements"];

    SPQuery spQuery = new SPQuery();
    spQuery.Query = "<Where> <Eq> <FieldRef Name='Title' /> <Value Type='Text'></Value> </Eq> </Where>";
    spQuery.Query = "";
    spQuery.RowLimit = 3;

    // Show item in text box  
    SPListItemCollection oListCollection = list.GetItems(spQuery);

    var editformUrl = list.Forms[PAGETYPE.PAGE_EDITFORM].ServerRelativeUrl;
    var table = new Table();
    Controls.Add(table);
    table.GridLines = GridLines.Both;
    foreach (SPListItem item in oListCollection)
    {
        var fullEditUrl = String.Format("{0}?ID={1}&source={2}",
            editformUrl,
            item.ID,
            HttpUtility.UrlEncode(HttpContext.Current.Request.Url.OriginalString));

        var row = new TableRow();
        table.Rows.Add(row);
        row.Attributes["onclick"] = string.Format("window.location='{0}';", fullEditUrl);
        row.Style[HtmlTextWriterStyle.Cursor] = "Pointer";

        var titleCell = new TableCell();
        row.Cells.Add(titleCell);
        titleCell.Controls.Add(new LiteralControl(string.Format("<a href='{1}'><span style='color:lime;'>{0}</span></a>", item.Title, fullEditUrl)));

        var imageCell = new TableCell();
        row.Cells.Add(imageCell);
        if (item.Attachments.Count > 0)
        {
            imageCell.Controls.Add(new HyperLink()
            {
                ImageUrl = SPUrlUtility.CombineUrl(item.Attachments.UrlPrefix, item.Attachments[0]),
                NavigateUrl = fullEditUrl
            });
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top