0

I am trying to develop a custom visual web part to display the latest 5 announcements with the images that are attached from the OOTB announcement lsit.I have a repeater control with an Item Template to render sharepoint list items. I am trying to understand the right process and syntax to bind data to my item template controls. Here is my ascx page:

And here is the code behind:

         SPWeb web = SPContext.Current.Web;
         SPList list = web.Lists["Announcements"];

        SPQuery spQuery = new SPQuery();
        spQuery.Query = " <Where><IsNotNull><FieldRef Name='Title' /></IsNotNull></Where> <OrderBy> <FieldRef Name='ID' Ascending='False' /> </OrderBy> ";
        spQuery.RowLimit = 5;
        SPListItemCollection oListCollection = list.GetItems(spQuery);


        repeatMyAnnouncements.ItemDataBound += new RepeaterItemEventHandler(repeatMyAnnouncements_ItemDataBound);
        repeatMyAnnouncements.DataSource = oListCollection; 
        repeatMyAnnouncements.DataBind();
    }

//My understanding beyond this is severly limited and I just cant seem to wrap my head around how to add my Announcement List Title(as a link to edit) and the images.

      private void repeatMyAnnouncements_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        HyperLink hypImageEditLink = (HyperLink)e.Item.FindControl("hypImageEditLink");
        HyperLink hypTextEditLink = (HyperLink)e.Item.FindControl("hypTextEditLink");
        SPListItem item = (SPListItem)e.Item.DataItem;
        string url = (string) item["Url"];

        hypImageEditLink.NavigateUrl = data;
        hypTextEditLink.NavigateUrl = data;
        hypTextEditLink.Text = "Link";

        //TODO: set image url
        string imageUrl= item.Attachments.UrlPrefix;

    }

if there are some good links that can help me get a basic understanding of the process and syntax please do mention as well. Thanks

@Brian: Here is the ascx code I have: ASCX code

4
  • Can you explain "add my Announcement List Title(as a link to edit) and Images".
    – sssreddy
    Commented Mar 21, 2012 at 17:41
  • I want to retrieve the Title and attachmetns(in my case images) and display them in a custom web part. The web part will basically have the a repeater control as illustrated in the code whic shows the image and the title below it. I hope this makes it cleare. Thanks
    – user7400
    Commented Mar 21, 2012 at 18:11
  • Hi I am looking to do something similar but even more simple, just display a SP list in a repeater. Did you get to the bottom of how to get the data to display in the ASCX page?
    – Stephen
    Commented Jul 13, 2012 at 12:00
  • You should probably start a new Question instead of commenting on this old one Commented Jul 13, 2012 at 12:14

1 Answer 1

2

OK, the short answer is this, but please continue reading on past the short answer...The SPAttachmentCollection contains a list of "strings" that represent the attachment file names. You were very close.

private void repeatMyAnnouncements_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    HyperLink hypImageEditLink = (HyperLink)e.Item.FindControl("hypImageEditLink");
    HyperLink hypTextEditLink = (HyperLink)e.Item.FindControl("hypTextEditLink");
    SPListItem item = (SPListItem)e.Item.DataItem;
    SPAttachmentCollection attachments = item.Attachments
    string url = (string) item["Url"];

    hypImageEditLink.NavigateUrl = data;
    hypTextEditLink.NavigateUrl = data;
    hypTextEditLink.Text = "Link";

    //start by setting to a well known image in a well known location, 
    //such as _layouts (if you are in a farm solution)
    string imageUrl = item.Web.Url + "/_layouts/images/defaultannouncementimage.png"
    if (attachments.Count > 0)
    {
        //TODO: handle more than one attachment???
        imageUrl= attachments.UrlPrefix + attachments[0]
    }
}

This said, you might consider creating a Picture Library to house your pictures. Picture Libraries give you some added benefits of maintaining thumbnail versions of your pictures. You could then reference the thumbnail image from a (newly added) Hyperlink field in the Announcements library. This Hyperlink field (if the Image format is selected), then has native rendering for the image within any list view. This may eliminate the need for a custom web part all together (and less code for you to maintain). Depending on how much control you give your end users for uploading the "images", you are potentially saving on bandwidth also because you are referncing a thumbnail which is likely smaller than the original image. If the field is another field on the list item, you also have the ability to require users to enter the information (whereas attachments are not required).

If the out of the box view styles aren't giving you a look/feel that you want, you can customize the XSL of the web part using SharePoint Designer: http://msdn.microsoft.com/en-us/library/ff630941.aspx. Alternatively, you can do some further reading to figure how this can be done in a manner that is repeatable (i.e. packagable in a feature): http://msdn.microsoft.com/en-us/library/ff806162.aspx

The downside to this approach is that the users need to be trained to upload images into a separate library instead of simply setting an attachment. This could be overcome by implementing some logic in a custom coded Event Receiver or Workflow. You would simply automate the process of extracing any Attachment(s) and putting them into a known library, updating the list item's Hyperlink field (assuming you add one), and deleting the attachments (no need to store them twice).

UPDATE: Forgot to address the part about getting the link to Edit the item. This will likely work in your scenario, but could be different if you started changing settings in your content types, etc.

SPList list = item.List;
string editUrl = list.DefaultEditFormUrl + "?ID=" + item.ID.ToString();

Again, this is much easier by simply modifying the XSL using an XSLT List View Web Part. In fact there is an Out of the Box field that gives you access to the Edit item link while displaying the title. You definitely do not NEED a custom web part for this, and if you are putting this on a home page that gets lots of traffic, I'd definitely think about the custom XSL approach.

3
  • Hey Brian Thanks for all the good info! I thought I would show you the ascx page. Could you take a look at it and let me know as to how I could finalize all the code above to work in sync and render my web part? I like the idea of the picture library. Does it automatically convert user images into specific sized thumbnails?
    – user7400
    Commented Mar 22, 2012 at 13:28
  • Essentially, yes, a Picture Library will automatically create two additional images beyond the source image. A "Preview" and "Thumbnail" image. Please see this reference for how the thumbnails work. It's based on a Jquery example, but explains the concepts behind how Picture Libraries store the smaller images. It also demonstrates that you don't need server side code (i.e. Web Part) to display things: weblogs.asp.net/bsimser/archive/2011/09/10/…
    – Brian
    Commented Mar 22, 2012 at 19:50
  • I've answered all the questions around how to get at the data via the SharePoint API. Everything else is standard ASP.NET binding and there are plenty of examples on how to use a repeater online. If you want me to write it for you, please contact me offline so we can negotiate a bill rate. :)~
    – Brian
    Commented Mar 22, 2012 at 19:53

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.