Skip to content

Instantly share code, notes, and snippets.

@xdumaine
Last active December 25, 2015 19:19
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/7026570 to your computer and use it in GitHub Desktop.
Save xdumaine/7026570 to your computer and use it in GitHub Desktop.
Resolve a web part by list name in SharePoint
// called from code behind
Utilities.ResolveWebPartList("MyListName", MyListWebPart);
// In a utilities class
internal static void ResolveWebPartList(string listName, XsltListViewWebPart listViewWebPart)
{
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));
}
listViewWebPart.ListId = theList.ID;
listViewWebPart.ListUrl = theList.DefaultViewUrl;
listViewWebPart.ViewGuid = theList.DefaultView.ID.ToString();
listViewWebPart.WebId = web.ID;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment