<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://www.dotnetmafia.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Corey Roth [MVP] : LINQ, SharePoint, LINQ to XML</title><link>http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/LINQ/SharePoint/LINQ+to+XML/default.aspx</link><description>Tags: LINQ, SharePoint, LINQ to XML</description><dc:language>en</dc:language><generator>CommunityServer 2007.1 (Build: 20917.1142)</generator><item><title>A simple way to programmatically create SharePoint security groups</title><link>http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2010/03/08/a-simple-way-to-programmatically-create-sharepoint-security-groups.aspx</link><pubDate>Mon, 08 Mar 2010 17:12:50 GMT</pubDate><guid isPermaLink="false">ceb7fe2a-c56b-4d85-99e6-8dd548580538:2930</guid><dc:creator>CoreyRoth</dc:creator><slash:comments>10</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://www.dotnetmafia.com/blogs/dotnettipoftheday/rsscomments.aspx?PostID=2930</wfw:commentRss><comments>http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2010/03/08/a-simple-way-to-programmatically-create-sharepoint-security-groups.aspx#comments</comments><description>&lt;p&gt;When it comes to SharePoint deployments, I try to automate everything I can.&amp;#160; I don’t like manual steps especially when it comes to setting up security.&amp;#160; A common task when deploying any sites is setting up security in some manner.&amp;#160; Today I am going to cover how to easily store definitions your SharePoint security groups in an XML file.&amp;#160; We’ll use LINQ to XML to make reading the file a breeze, and then we’ll use the SharePoint object model to create the groups and add users (or AD groups).&amp;#160;&amp;#160; I’ve blogged on &lt;a href="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/08/25/how-to-create-a-group-in-sharepoint.aspx"&gt;how to create a group&lt;/a&gt; before, but we’re going to take this a step further by giving you code that you can easily add to a feature receiver or console application.&amp;#160; First let’s take a look at the XML file we’re going to use.&lt;/p&gt;  &lt;div style="font-family:consolas;background:black;color:white;font-size:13pt;font-weight:bold;"&gt;   &lt;p style="margin:0px;"&gt;&amp;lt;?&lt;span style="color:#cc7832;"&gt;xml&lt;/span&gt; &lt;span style="color:silver;"&gt;version&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;1.0&lt;/span&gt;&amp;quot; &lt;span style="color:silver;"&gt;encoding&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;utf-8&lt;/span&gt;&amp;quot; ?&amp;gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;lt;&lt;span style="color:#cc7832;"&gt;Groups&lt;/span&gt;&amp;gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160; &amp;lt;&lt;span style="color:#cc7832;"&gt;Group&lt;/span&gt; &lt;span style="color:silver;"&gt;Name&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;My Custom Read Group&lt;/span&gt;&amp;quot; &lt;span style="color:silver;"&gt;Owner&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;SHAREPOINT\GroupOwner&lt;/span&gt;&amp;quot; &lt;span style="color:silver;"&gt;Description&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;Readonly Permission Group&lt;/span&gt;&amp;quot; &lt;span style="color:silver;"&gt;PermissionLevel&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;Read&lt;/span&gt;&amp;quot;&amp;gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;span style="color:#cc7832;"&gt;User&lt;/span&gt; &lt;span style="color:silver;"&gt;Name&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;SHAREPOINT\TestUser1&lt;/span&gt;&amp;quot; /&amp;gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;span style="color:#cc7832;"&gt;User&lt;/span&gt; &lt;span style="color:silver;"&gt;Name&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;SHAREPOINT\TestGroup1&lt;/span&gt;&amp;quot; /&amp;gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;span style="color:#cc7832;"&gt;User&lt;/span&gt; &lt;span style="color:silver;"&gt;Name&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;SHAREPOINT\TestGroup2&lt;/span&gt;&amp;quot; /&amp;gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160; &amp;lt;/&lt;span style="color:#cc7832;"&gt;Group&lt;/span&gt;&amp;gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160; &amp;lt;&lt;span style="color:#cc7832;"&gt;Group&lt;/span&gt; &lt;span style="color:silver;"&gt;Name&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;My Custom Contributors Group&lt;/span&gt;&amp;quot; &lt;span style="color:silver;"&gt;Owner&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;SHAREPOINT\GroupOwner&lt;/span&gt;&amp;quot; &lt;span style="color:silver;"&gt;Description&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;Contributors Permission Group&lt;/span&gt;&amp;quot; &lt;span style="color:silver;"&gt;PermissionLevel&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;Contribute&lt;/span&gt;&amp;quot;&amp;gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;span style="color:#cc7832;"&gt;User&lt;/span&gt; &lt;span style="color:silver;"&gt;Name&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;SHAREPOINT\TestGroup3&lt;/span&gt;&amp;quot; /&amp;gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160; &amp;lt;/&lt;span style="color:#cc7832;"&gt;Group&lt;/span&gt;&amp;gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;lt;/&lt;span style="color:#cc7832;"&gt;Groups&lt;/span&gt;&amp;gt;&lt;/p&gt; &lt;/div&gt;   &lt;p&gt;In this file, I am defining two SharePoint groups.&amp;#160; One that will have read access and one that will have contribute access.&amp;#160; I store the required information needed by the Add method on the SPGroupCollection object.&amp;#160; I then have one or more User elements with the name of my Active Directory user or group.&amp;#160; I tried to keep my XML schema pretty simple.&amp;#160; You can customize it obviously how you want, you would just have to alter your LINQ queries.&amp;#160; &lt;/p&gt;  &lt;p&gt;Let’s take a look at the code we need to make this happen.&amp;#160; I won’t go into as much detail of the object model since I went into it pretty well on my last post.&amp;#160; We’ll just focus on how we use LINQ to XML to read the information we need and then have it create our groups.&amp;#160; My method is called &lt;em&gt;CreateGroups&lt;/em&gt; and it takes an SPWeb object and a string with the filename of the XML document.&lt;/p&gt;  &lt;div style="font-family:consolas;background:black;color:white;font-size:13pt;font-weight:bold;"&gt;   &lt;p style="margin:0px;"&gt;&lt;span style="color:#cc7832;"&gt;private&lt;/span&gt; &lt;span style="color:#cc7832;"&gt;void&lt;/span&gt; CreateGroups(&lt;span style="color:#ffc66d;"&gt;SPWeb&lt;/span&gt; currentSite, &lt;span style="color:#cc7832;"&gt;string&lt;/span&gt; groupsFilename)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;{&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;// get the xml document from the feature folder&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#ffc66d;"&gt;XDocument&lt;/span&gt; groupsXml = &lt;span style="color:#ffc66d;"&gt;XDocument&lt;/span&gt;&lt;span style="font-weight:normal;"&gt;.Load(groupsFilename);&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;// create a new anoynmous type with the group data&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;var&lt;/span&gt; groups = &lt;span style="color:#cc7832;"&gt;from&lt;/span&gt; sharePointGroup &lt;span style="color:#cc7832;"&gt;in&lt;/span&gt; groupsXml.Root.Elements(&lt;span style="color:#a5c25c;"&gt;&amp;quot;Group&amp;quot;&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;select&lt;/span&gt; &lt;span style="color:#cc7832;"&gt;new&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Name = sharePointGroup.Attribute(&lt;span style="color:#a5c25c;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;).Value,&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Owner = sharePointGroup.Attributes(&lt;span style="color:#a5c25c;"&gt;&amp;quot;Owner&amp;quot;&lt;/span&gt;).Any() ? sharePointGroup.Attribute(&lt;span style="color:#a5c25c;"&gt;&amp;quot;Owner&amp;quot;&lt;/span&gt;).Value : &lt;span style="color:#cc7832;"&gt;null&lt;/span&gt;,&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Description = sharePointGroup.Attributes(&lt;span style="color:#a5c25c;"&gt;&amp;quot;Description&amp;quot;&lt;/span&gt;).Any() ? sharePointGroup.Attribute(&lt;span style="color:#a5c25c;"&gt;&amp;quot;Description&amp;quot;&lt;/span&gt;).Value : &lt;span style="color:#cc7832;"&gt;string&lt;/span&gt;&lt;span style="font-weight:normal;"&gt;.Empty,&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; PermissionLevel = sharePointGroup.Attributes(&lt;span style="color:#a5c25c;"&gt;&amp;quot;PermissionLevel&amp;quot;&lt;/span&gt;).Any() ? sharePointGroup.Attribute(&lt;span style="color:#a5c25c;"&gt;&amp;quot;PermissionLevel&amp;quot;&lt;/span&gt;).Value : &lt;span style="color:#cc7832;"&gt;null&lt;/span&gt;,&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Users = sharePointGroup.Elements(&lt;span style="color:#a5c25c;"&gt;&amp;quot;User&amp;quot;&lt;/span&gt;).Any() ? sharePointGroup.Elements(&lt;span style="color:#a5c25c;"&gt;&amp;quot;User&amp;quot;&lt;/span&gt;) : &lt;span style="color:#cc7832;"&gt;null&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; };&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;// iterate through the groups and create the groups&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;foreach&lt;/span&gt; (&lt;span style="color:#cc7832;"&gt;var&lt;/span&gt; sharePointGroup &lt;span style="color:#cc7832;"&gt;in&lt;/span&gt; groups)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;// only create the group if it does not exist&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;if&lt;/span&gt; (!ContainsGroup(currentSite.SiteGroups, sharePointGroup.Name))&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;// add the owner to the web site users&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; currentSite.EnsureUser(sharePointGroup.Owner);&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;// add the group&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; currentSite.SiteGroups.Add(sharePointGroup.Name, currentSite.SiteUsers[sharePointGroup.Owner],&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; currentSite.SiteUsers[sharePointGroup.Owner], sharePointGroup.Description);&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;// add the users to the group&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; AddUsersToGroup(sharePointGroup.Name, sharePointGroup.Users, currentSite, sharePointGroup.PermissionLevel);&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin:0px;"&gt;}&lt;/p&gt; &lt;/div&gt;   &lt;p&gt;This seems like kind of a big method at first, but it’s really not that bad.&amp;#160; To keep things simple, I haven’t included any exception handling code.&amp;#160; We are really just querying the XML document, iterating through each group element inside of it, creating the groups, and then adding the users to the group.&amp;#160; The first line of code just creates an XDocument object.&amp;#160; We then construct a LINQ to XML query.&amp;#160; What we want is to return data from each Group element in the document.&amp;#160; The Add method doesn’t like nulls, so we check for them and use string.Empty if the value does not exist in the file.&amp;#160; The one case where I don’t do this is for the Name of the group.&amp;#160; If that is not present, I would rather the process throw an exception.&amp;#160; As for the Users assigned to the group, I grab all of them and add them to our anonymous type like this.&lt;/p&gt;   &lt;div style="font-family:consolas;background:black;color:white;font-size:13pt;font-weight:bold;"&gt;   &lt;p style="margin:0px;"&gt;Users = sharePointGroup.Elements(&lt;span style="color:#a5c25c;"&gt;&amp;quot;User&amp;quot;&lt;/span&gt;).Any() ? sharePointGroup.Elements(&lt;span style="color:#a5c25c;"&gt;&amp;quot;User&amp;quot;&lt;/span&gt;) : &lt;span style="color:#cc7832;"&gt;null&lt;/span&gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;This gives us an &lt;em&gt;IEnumerable&amp;lt;XElement&amp;gt;&lt;/em&gt; that we can pass to a method later to add each Active Directory user (or group) to the SharePoint group.&amp;#160; Once we execute the query, we iterate through each group element.&amp;#160; The first thing we have to do is make sure that the group does not exist.&amp;#160; Of course there is no way to do that other than using the &lt;a href="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/03/03/using-an-extension-method-to-check-if-an-item-exists-in-a-sharepoint-collection.aspx"&gt;try/catch&lt;/a&gt; technique.&amp;#160; I will usually wrap this in an extension method, but for today’s purpose, we’ll just call a method to check.&lt;/p&gt;  &lt;div style="font-family:consolas;background:black;color:white;font-size:13pt;font-weight:bold;"&gt;   &lt;p style="margin:0px;"&gt;&lt;span style="color:#cc7832;"&gt;private&lt;/span&gt; &lt;span style="color:#cc7832;"&gt;bool&lt;/span&gt; ContainsGroup(&lt;span style="color:#ffc66d;"&gt;SPGroupCollection&lt;/span&gt; groupCollection, &lt;span style="color:#cc7832;"&gt;string&lt;/span&gt; index)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;{&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;try&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#ffc66d;"&gt;SPGroup&lt;/span&gt; testGroup = groupCollection[index];&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;return&lt;/span&gt; &lt;span style="color:#cc7832;"&gt;true&lt;/span&gt;;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;catch&lt;/span&gt; (&lt;span style="color:#ffc66d;"&gt;SPException&lt;/span&gt; e)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;return&lt;/span&gt; &lt;span style="color:#cc7832;"&gt;false&lt;/span&gt;;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin:0px;"&gt;}&lt;/p&gt; &lt;/div&gt;   &lt;p&gt;Lame I know.&amp;#160; I’m so happy there are ways to &lt;a href="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2009/10/21/checking-to-see-if-a-list-item-exists-in-sharepoint-2010.aspx"&gt;get around&lt;/a&gt; this in SharePoint 2010.&amp;#160; Then this starts to look like code from the previous post.&amp;#160; We call .EnsureUser to make sure the domain account of the group owner is registered with the site.&amp;#160; We then just call the Add method with the Name, Owner, default user, and description.&amp;#160; Again there is more info on the &lt;a href="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/08/25/how-to-create-a-group-in-sharepoint.aspx"&gt;previous post&lt;/a&gt; about that method call.&amp;#160; Assuming the group is created, we can then add the users to the group.&amp;#160; We call a new method &lt;em&gt;AddUsersToGroup&lt;/em&gt; which takes the groupName, the users element, an SPWeb, and the permission level.&amp;#160; &lt;/p&gt;  &lt;p&gt;The first thing we do is query the names of the Active Directory users (or groups).&amp;#160; Here we are just grabbing it from the Name attribute of the User element.&amp;#160; I probably could have condensed this query, but at least it’s easy to read.&amp;#160; We then add each user (or group) from the User elements to the group.&amp;#160; If you are curious about the empty parameters, take a look at the previous post.&amp;#160; If you are going to run into an exception, it’s going to be here.&amp;#160; If the group failed to be created or if the user does not exist (i.e.: you typed it in the XML file wrong), this line will throw an exception.&lt;/p&gt;  &lt;div style="font-family:consolas;background:black;color:white;font-size:13pt;font-weight:bold;"&gt;   &lt;p style="margin:0px;"&gt;&lt;span style="color:#cc7832;"&gt;private&lt;/span&gt; &lt;span style="color:#cc7832;"&gt;void&lt;/span&gt; AddUsersToGroup(&lt;span style="color:#cc7832;"&gt;string&lt;/span&gt; groupName, &lt;span style="color:#6897bb;"&gt;IEnumerable&lt;/span&gt;&lt;span style="font-weight:normal;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#ffc66d;"&gt;XElement&lt;/span&gt;&lt;span style="font-weight:normal;"&gt;&amp;gt; users, &lt;/span&gt;&lt;span style="color:#ffc66d;"&gt;SPWeb&lt;/span&gt; currentSite)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;{&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;// select the username from the xml document&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;var&lt;/span&gt; userList = &lt;span style="color:#cc7832;"&gt;from&lt;/span&gt; user &lt;span style="color:#cc7832;"&gt;in&lt;/span&gt; users&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;select&lt;/span&gt; &lt;span style="color:#cc7832;"&gt;new&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Name = user.Attribute(&lt;span style="color:#a5c25c;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;).Value&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; };&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;// add the users to the sharepoint group&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;foreach&lt;/span&gt; (&lt;span style="color:#cc7832;"&gt;var&lt;/span&gt; user &lt;span style="color:#cc7832;"&gt;in&lt;/span&gt; userList)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; currentSite.SiteGroups[groupName].AddUser(user.Name, &lt;span style="color:#cc7832;"&gt;string&lt;/span&gt;&lt;span style="font-weight:normal;"&gt;.Empty, user.Name, &lt;/span&gt;&lt;span style="color:#cc7832;"&gt;string&lt;/span&gt;&lt;span style="font-weight:normal;"&gt;.Empty);&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin:0px;"&gt;}&lt;/p&gt; &lt;/div&gt;   &lt;p&gt;Now, we’re almost done.&amp;#160; The last thing we need to do is set the permission level on the group.&amp;#160; This is where we specify whether the group has readonly, contribute, full control, etc access to the site.&amp;#160; Be sure and get the name on the permission level right otherwise you will get an exception.&amp;#160; I’ve also blogged about &lt;a href="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/04/02/how-to-assign-a-permission-level-to-a-sharepoint-group.aspx"&gt;how to assign permission levels&lt;/a&gt; before.&amp;#160; Today’s post is really just a great practical use of putting together the things I have posted on before.&lt;/p&gt;  &lt;div style="font-family:consolas;background:black;color:white;font-size:13pt;font-weight:bold;"&gt;   &lt;p style="margin:0px;"&gt;&lt;span style="color:#cc7832;"&gt;private&lt;/span&gt; &lt;span style="color:#cc7832;"&gt;void&lt;/span&gt; SetRoleDefinitionBinding(&lt;span style="color:#cc7832;"&gt;string&lt;/span&gt; groupName, &lt;span style="color:#ffc66d;"&gt;SPWeb&lt;/span&gt; currentSite, &lt;span style="color:#cc7832;"&gt;string&lt;/span&gt; permissionLevel)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;{&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;// add the read role definition to the site group&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#ffc66d;"&gt;SPRoleAssignment&lt;/span&gt; roleAssignment = &lt;span style="color:#cc7832;"&gt;new&lt;/span&gt; &lt;span style="color:#ffc66d;"&gt;SPRoleAssignment&lt;/span&gt;(currentSite.SiteGroups[groupName]);&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; roleAssignment.RoleDefinitionBindings.Add(currentSite.RoleDefinitions[permissionLevel]);&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; currentSite.RoleAssignments.Add(roleAssignment);&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; currentSite.Update();&lt;/p&gt;    &lt;p style="margin:0px;"&gt;}&lt;/p&gt; &lt;/div&gt;   &lt;p&gt;Effectively you create a new SPRoleAssignment by passing it a SPGroup object.&amp;#160; You then add a binding using the existing RoleDefinitions on the site.&amp;#160; You then add the assignment to the site and of course call .Update() so things get saved.&lt;/p&gt;  &lt;p&gt;That’s really all there is to it.&amp;#160; This is a great use of combining information from my previous posts into something that you can use everyday to set security on your sites.&amp;#160; How you execute this code is up to you.&amp;#160; I’ve used it in a feature receiver and in a console application before.&amp;#160; Setting up security through the UI is very slow and painful.&amp;#160; Once you create it on one server, there is no way to move it to another server and that’s not a lot of fun.&amp;#160; This should help you with that and eliminate those nasty manual steps in your deployment process.&amp;#160; &lt;/p&gt;&lt;img src="http://www.dotnetmafia.com/aggbug.aspx?PostID=2930" width="1" height="1"&gt;</description><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/SharePoint/default.aspx">SharePoint</category><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/LINQ+to+XML/default.aspx">LINQ to XML</category><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/LINQ/default.aspx">LINQ</category></item><item><title>Linq to XML and Deleting Lists on Feature Deactivation</title><link>http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2009/09/24/linq-to-xml-and-deleting-lists-on-feature-deactivation.aspx</link><pubDate>Thu, 24 Sep 2009 20:04:04 GMT</pubDate><guid isPermaLink="false">ceb7fe2a-c56b-4d85-99e6-8dd548580538:976</guid><dc:creator>CoreyRoth</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://www.dotnetmafia.com/blogs/dotnettipoftheday/rsscomments.aspx?PostID=976</wfw:commentRss><comments>http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2009/09/24/linq-to-xml-and-deleting-lists-on-feature-deactivation.aspx#comments</comments><description>&lt;p&gt;A few months ago, I discussed how to use LINQ to XML to parse your elements.xml file to &lt;a href="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2009/02/16/linq-to-xml-and-deleting-files-on-feature-deactivation.aspx"&gt;delete any files&lt;/a&gt; that you may have deployed on feature activation.&amp;#160; Today I have decided to reuse this concept to delete any lists that I have created when I deactivate a feature?&amp;#160; Now you may ask, “why would I want to delete a list when a feature is deactivated?&amp;#160; I’ll lose all of my data in that list!”&amp;#160; My answer is: Yes, of course you will, but sometimes when building a feature to deploy a list(s), you want to delete the list each time before you deploy a new version of it.&amp;#160; During development this is a huge time saver.&amp;#160; Right now, I am working on a feature that deploys four document libraries.&amp;#160; This means I have to manually delete each one.&amp;#160; That is a huge waste of time, but what is nice is that we can reuse the concept above with LINQ to XML and delete lists instead.&amp;#160; &lt;/p&gt;  &lt;p&gt;The code is quite similar.&amp;#160; Have a look.&amp;#160; We first, write some code to get the path to the elements.xml file.&amp;#160; This code snippet assumes, it is always named Elements.xml.&amp;#160; Maybe in the future, I will have it look in the feature.xml file, get all of the ElementManifest definitions and then delete whatever it finds in each file.&amp;#160; For now though, we assume, you can change the path as needed.&lt;/p&gt;   &lt;div style="font-family:consolas;background:black;color:white;font-size:13pt;font-weight:bold;"&gt;   &lt;p style="margin:0px;"&gt;&lt;span style="color:#cc7832;"&gt;public&lt;/span&gt; &lt;span style="color:#cc7832;"&gt;override&lt;/span&gt; &lt;span style="color:#cc7832;"&gt;void&lt;/span&gt; FeatureDeactivating(SPFeatureReceiverProperties properties)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;{&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;using&lt;/span&gt; (SPWeb currentSite = (SPWeb)properties.Feature.Parent)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;string&lt;/span&gt; elementsPath = &lt;span style="color:#cc7832;"&gt;string&lt;/span&gt;&lt;span style="font-weight:normal;"&gt;.Format(&lt;/span&gt;&lt;span style="color:#a31515;font-weight:normal;"&gt;@&amp;quot;{0}\FEATURES\{1}\Elements.xml&amp;quot;&lt;/span&gt;, SPUtility.GetGenericSetupPath(&lt;span style="color:#a5c25c;"&gt;&amp;quot;Template&amp;quot;&lt;/span&gt;), properties.Definition.DisplayName);&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; DeleteLists(currentSite, elementsPath);&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin:0px;"&gt;}&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;From here, it passes a reference to the current SPWeb and the path to the XML document.&amp;#160; The path to the elements.xml file is used to populate an XDocument which we can then query with LINQ to XML.&lt;/p&gt;   &lt;div style="font-family:consolas;background:black;color:white;font-size:13pt;font-weight:bold;"&gt;   &lt;p style="margin:0px;"&gt;&lt;span style="color:#cc7832;"&gt;private&lt;/span&gt; &lt;span style="color:#cc7832;"&gt;void&lt;/span&gt; DeleteLists(SPWeb currentSite, &lt;span style="color:#cc7832;"&gt;string&lt;/span&gt; elementsPath)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;{&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;try&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; XDocument elementsXml = XDocument.Load(elementsPath);&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; XNamespace sharePointNamespace = &lt;span style="color:#a5c25c;"&gt;&amp;quot;http://schemas.microsoft.com/sharepoint/&amp;quot;&lt;/span&gt;;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;// get each URL to each list&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;var&lt;/span&gt; listInstances = &lt;span style="color:#cc7832;"&gt;from&lt;/span&gt; module &lt;span style="color:#cc7832;"&gt;in&lt;/span&gt; elementsXml.Root.Elements(sharePointNamespace + &lt;span style="color:#a5c25c;"&gt;&amp;quot;ListInstance&amp;quot;&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;select&lt;/span&gt; &lt;span style="color:#cc7832;"&gt;new&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ListUrl = (module.Attributes(&lt;span style="color:#a5c25c;"&gt;&amp;quot;Url&amp;quot;&lt;/span&gt;).Any()) ? module.Attribute(&lt;span style="color:#a5c25c;"&gt;&amp;quot;Url&amp;quot;&lt;/span&gt;).Value : &lt;span style="color:#cc7832;"&gt;null&lt;/span&gt;,&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; };&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;// iterate through each list and delete it&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;foreach&lt;/span&gt; (&lt;span style="color:#cc7832;"&gt;var&lt;/span&gt; listInstance &lt;span style="color:#cc7832;"&gt;in&lt;/span&gt; listInstances)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;try&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; SPList currentList = currentSite.GetList(&lt;span style="color:#cc7832;"&gt;string&lt;/span&gt;&lt;span style="font-weight:normal;"&gt;.Format(&lt;/span&gt;&lt;span style="color:#a5c25c;"&gt;&amp;quot;{0}/{1}&amp;quot;&lt;/span&gt;, currentSite.Url, listInstance.ListUrl));&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; currentList.Delete();&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;catch&lt;/span&gt; (System.IO.&lt;span style="color:#ffc66d;"&gt;FileNotFoundException&lt;/span&gt; e)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;// this exception is thrown if the list does not exist&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;// update the site&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; currentSite.Update();&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;catch&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin:0px;"&gt;}&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;This code is quite similar to that used in my other post, however instead of looking for Module elements we are looking for &lt;em&gt;ListInstance&lt;/em&gt; elements.&amp;#160; It creates a list of those Urls to each list and iterates through them.&amp;#160; For each list it finds, it deletes the list.&amp;#160;&amp;#160; I’ve written code in the past to manually delete lists, I create by name.&amp;#160; That works fine, but the name of each list has to be maintained.&amp;#160; Now, I just attach this to any feature I am working on and it deletes any list (or lists) that I throw at it.&amp;#160; More than likely this isn’t something you want once you go to production, but it will save you a ton of time during development.&lt;/p&gt;&lt;img src="http://www.dotnetmafia.com/aggbug.aspx?PostID=976" width="1" height="1"&gt;</description><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/SharePoint/default.aspx">SharePoint</category><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/LINQ+to+XML/default.aspx">LINQ to XML</category><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/Feature/default.aspx">Feature</category><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/Enterprise+Library/default.aspx">Enterprise Library</category></item><item><title>How I used LINQ to XML to Create Document Library Folders</title><link>http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/01/17/how-i-used-linq-to-xml-to-create-document-library-folders.aspx</link><pubDate>Thu, 17 Jan 2008 23:29:24 GMT</pubDate><guid isPermaLink="false">ceb7fe2a-c56b-4d85-99e6-8dd548580538:13</guid><dc:creator>C-Dog's .NET Tip of the Day</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://www.dotnetmafia.com/blogs/dotnettipoftheday/rsscomments.aspx?PostID=13</wfw:commentRss><comments>http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/01/17/how-i-used-linq-to-xml-to-create-document-library-folders.aspx#comments</comments><description>I have been doing a lot of document library work lately and in my work I had a need to create a series of subfolders in the document library using a particular custom content type. This meant I was going to need to write a little code. I decided to keep...(&lt;a href="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/01/17/how-i-used-linq-to-xml-to-create-document-library-folders.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://www.dotnetmafia.com/aggbug.aspx?PostID=13" width="1" height="1"&gt;</description><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/SharePoint/default.aspx">SharePoint</category><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/LINQ+to+XML/default.aspx">LINQ to XML</category><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/Document+Library/default.aspx">Document Library</category><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/LINQ/default.aspx">LINQ</category></item></channel></rss>