SPWeb Extension Methods

SPWeb root = FindRootWeb();

void RunWithUnsafeUpdates(Action<SPWeb> action)
Runs a piece of code with unsafe updates set to true and then returns to the previous value.
web.RunWithUnsafeUpdates(web2 =>
{
  // web2 will have AllowUnsafeUpdates set to true for all the code that runs between the {...} curly brackets
});

bool TryFindListByName(string name, out SPList list)
Tries to find a list within the SPWeb using the name (internal name). A return value indicates whether it succeeded or not.
SPList list;

if(root.TryFindListByName("ListInternalName", out list))
{
  // list has been found
}

SPList FindListByName(string name)
Searches for a list within the SPWeb using the name (internal name) of the list. The list can be null and the method can throw exceptions.
try
{
  SPList list = root.FindListByName("ListInternalName");
}
catch(...)

SPListTemplate FindTemplateByInternalName(string name)
Searches for a list template within the SPWeb using the name (internal name) of the list template.

LibraryBuilder CreateLibrary(string name)
Creates an instance of the LibraryBuilder class.

bool ContainsContentType(SPContentTypeId id)
Determines whether the specified SPWeb contains a content type.

bool TryFindContentType(SPContentTypeId id, out SPContentType result)
Determines whether the specified SPWeb contains a content type and returns that content type if it does as an out parameter.
SPContentType masterpage;
if(root.TryFindContentType(SPBuiltInContentTypeId.MasterPage, out masterpage))
{
  // we have found the content type
}

Last edited Feb 15, 2010 at 6:01 PM by jole, version 5

Comments

No comments yet.