Last active
December 25, 2015 19:19
-
-
Save xdumaine/7026570 to your computer and use it in GitHub Desktop.
Resolve a web part by list name in SharePoint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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