Skip to content

Instantly share code, notes, and snippets.

@xdumaine
Last active December 26, 2015 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xdumaine/7139388 to your computer and use it in GitHub Desktop.
Save xdumaine/7139388 to your computer and use it in GitHub Desktop.
utility method for resolving a ListFormWebPart on a custom page
// in code behind (null checking/exception catching omitted for clarity)
var listItemId = int.Parse(Request["ID"]);
Utilities.ResolveListFormWebPart("MyListName", listItemId, MyListFormWebPart);
// in utilities class
internal static void ResolveListFormWebPart(string listName, int listItemId, ListFormWebPart listFormWebPart)
{
using (var site = new SPSite(SPContext.Current.Web.Url))
using (var web = site.OpenWeb())
{
var theList = web.Lists[listName];
if (theList == null)
{
throw new NullReferenceException(string.Format("The specified list '{0}' does not exist in the site '{1}'.", listName, SPContext.Current.Web.Url));
}
listFormWebPart.ListId = theList.ID;
listFormWebPart.ListItemId = listItemId;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment