in

Corey Roth and Friends Blogs

Group site for developer blogs dealing with (usually) Ionic, .NET, SharePoint, Office 365, Mobile Development, and other Microsoft products, as well as some discussion of general programming related concepts.

Kyle Kelin on .Net

Using Extension Methods to Make SharePoint More Palatable

On my current project I have been using a lot of extension methods to hide things I don't like about the SharePoint API. For one I hate that the only way to determine if something is in a list, web, or group is by trying to access the item and then catch the exception. Yuck. Here is an extension method I wrote to test if a group exists in a site or not.

 

public static bool Contains(this SPGroupCollection groups, string groupName)

{

try

{

SPGroup group = groups[groupName];

return true;

}

catch (Exception)

{

 

return false;

}

}

 

And here is how I use the extension method.

 

if (!committeesRootWeb.SiteGroups.Contains(groupName))

{

committeesRootWeb.SiteGroups.Add(groupName, member, user, groupName);

}

 

For a little more info check out this post:

http://johnholliday.net/post/2008/10/22/Streamline-Your-SharePoint-Code-Using-Extension-Methods.aspx

Comments

No Comments

Leave a Comment

(required)  
(optional)
(required)  
Add

About KyleKelin

Kyle Kelin has been implementing software in the Microsoft space for the past 6 years mainly as a consultant. His interests are SharePoint, .NET, JQuery, and Silverlight.
2019.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems