Quantcast
Channel: SharePoint – Be Awesome @
Viewing all articles
Browse latest Browse all 19

ItemFieldIterator: Display Content from Different Web

$
0
0

The List Fields Iterator control provides an easy way to present list information dynamically on your page.  Just point it to the ListId, set the control mode to New or if the item exists, Display or Edit while setting the ItemId.  You do not need to know the names of the columns in your list, unless you do not want to display.  This works great provided the list and the page are on the same SPWeb.  The ListFieldIterator does have a Web property, but this is a Get or Read-only property, so there was no way to directly set this on the control.  However, you can indirectly set it.

The ListFieldIterator contains a property called ItemContext.  This property can be set with an SPContext object during the Page_Init method of the application page.  There are a number of overloads for the SPContext.GetContext method and you can see them here.  I picked the one that worked best for my scenario., which in this case is am SPWeb and ListId in my context.

  1. using (SPSite oSPSSite = new SPSite(SPContext.Current.Site.ID))
  2. {
  3.     using (SPWeb oSPWeb = oSPSSite.OpenWeb(string.Concat(oSPSSite.ServerRelativeUrl, "/mySite")))
  4.     {
  5.         Guid ListId = oSPWeb.GetList(string.Concat(oSPWeb.ServerRelativeUrl, "/", ListUrl)).ID;
  6.         NewDocumentItem.ItemContext = SPContext.GetContext(Context, 0, ListId, oSPWeb);
  7.     }
  8. }

This is now the only property I needed to set for the control.  I was setting the list id  and item properties on the ListFieldIterator when the list was on the same SPWeb.  The context now handles this.  In working with this control, it looks like setting the list and item properties in addition to the ItemContext property confused the ListFieldIterator.  It seemed to forget the ItemContext property and try to resolve the ListId based on the SPContext.Current properties.

One other note is that if you are saving new or updated information to the list, you will need to run the new or update code inside the SPSecurity.RunWithElevatedPriviledges block.  This is because this portion of the page has a different context than the Form tag on the page, which is a security issue.



Viewing all articles
Browse latest Browse all 19

Latest Images

Trending Articles





Latest Images