Profile cover photo
Profile photo
Rich Ross
72 followers
72 followers
About
Rich's posts

Post has attachment

Post has attachment
Can you answer this question?

Post has attachment

Post has attachment

Post has attachment

Post has attachment

Post has attachment

Post has attachment

ItemFieldIterator: Display Content from Different Web

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.



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



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.

Post has attachment
Wait while more posts are being loaded