<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://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 to XML</title><link>http://dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/LINQ+to+XML/default.aspx</link><description>Tags: 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://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://dotnetmafia.com/blogs/dotnettipoftheday/rsscomments.aspx?PostID=2930</wfw:commentRss><comments>http://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://dotnetmafia.com/aggbug.aspx?PostID=2930" width="1" height="1"&gt;</description><category domain="http://dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/SharePoint/default.aspx">SharePoint</category><category domain="http://dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/LINQ+to+XML/default.aspx">LINQ to XML</category><category domain="http://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://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://dotnetmafia.com/blogs/dotnettipoftheday/rsscomments.aspx?PostID=976</wfw:commentRss><comments>http://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://dotnetmafia.com/aggbug.aspx?PostID=976" width="1" height="1"&gt;</description><category domain="http://dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/SharePoint/default.aspx">SharePoint</category><category domain="http://dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/LINQ+to+XML/default.aspx">LINQ to XML</category><category domain="http://dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/Feature/default.aspx">Feature</category><category domain="http://dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/Enterprise+Library/default.aspx">Enterprise Library</category></item><item><title>Using LINQ to XML to access the data of a form saved to a form library in a workflow</title><link>http://dotnetmafia.com/blogs/dotnettipoftheday/archive/2009/05/06/using-linq-to-xml-to-access-the-data-of-a-form-saved-to-a-form-library-in-a-workflow.aspx</link><pubDate>Wed, 06 May 2009 21:40:37 GMT</pubDate><guid isPermaLink="false">ceb7fe2a-c56b-4d85-99e6-8dd548580538:900</guid><dc:creator>CoreyRoth</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetmafia.com/blogs/dotnettipoftheday/rsscomments.aspx?PostID=900</wfw:commentRss><comments>http://dotnetmafia.com/blogs/dotnettipoftheday/archive/2009/05/06/using-linq-to-xml-to-access-the-data-of-a-form-saved-to-a-form-library-in-a-workflow.aspx#comments</comments><description>&lt;p&gt;As I have mention in the past LINQ to XML is a great way to manipulate the XML data out of an &lt;a href="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2009/04/29/code-samples-from-my-short-talk-about-linq-to-xml.aspx"&gt;InfoPath form&lt;/a&gt;.&amp;#160; I thought I would show how you can also use the same technique to work with the data of a form saved to a form library which launches a workflow (although you could apply this technique to the association or task forms as well).&amp;#160; The first thing we need to do is get the XML of the InfoPath initiation form, before we can work with it.&lt;/p&gt;  &lt;p&gt;For the purpose of my example, my OnWorkflowActivated activity writes to a public SPWorkflowActivationProperties property called &lt;em&gt;workflowProperties&lt;/em&gt;.&amp;#160; In my Invoked event handling method of my OnWorkflowActivated activity, we use the Item object on my workflowProperties to open the file and get the XML.&amp;#160; You can then use the File object along with OpenBinary() to get the raw bytes of the 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;byte&lt;/span&gt;[] xmlBytes = workflowProperties.Item.File.OpenBinary();&lt;/p&gt; &lt;/div&gt;   &lt;p&gt;You can then takes those bytes and read them into a regular string.&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;string&lt;/span&gt; xmlString = System.Text.&lt;span style="color:#ffc66d;"&gt;Encoding&lt;/span&gt;&lt;span style="font-weight:normal;"&gt;.UTF8.GetString(xmlBytes);&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#cc7832;"&gt;if&lt;/span&gt; (xmlString[&lt;span style="color:#6897bb;"&gt;0&lt;/span&gt;] == (&lt;span style="color:#cc7832;"&gt;char&lt;/span&gt;)&lt;span style="color:#6897bb;"&gt;0xfeff&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; xmlString = xmlString.Substring(&lt;span style="color:#6897bb;"&gt;1&lt;/span&gt;);&lt;/p&gt; &lt;/div&gt;   &lt;p&gt;If you are curious about the 0xfeff business that is used to deal with the byte-order mark character in Unicode.&amp;#160; I picked up this trick from another post somewhere, but I can’t remember where currently.&amp;#160; &lt;/p&gt;  &lt;p&gt;Now we need to get the XML string into an XDocument class so that we can query it and manipulate it.&amp;#160; Since we have a string, we will need to create a StringReader to read it.&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:#ffc66d;"&gt;XDocument&lt;/span&gt; documentsXml = &lt;span style="color:#ffc66d;"&gt;XDocument&lt;/span&gt;&lt;span style="font-weight:normal;"&gt;.Load(&lt;/span&gt;&lt;span style="color:#cc7832;"&gt;new&lt;/span&gt; System.IO.&lt;span style="color:#ffc66d;"&gt;StringReader&lt;/span&gt;(xmlString));&lt;/p&gt; &lt;/div&gt;   &lt;p&gt;If you wanted to work with the Association form instead of the form that started the workflow, you would use a line like the following.&amp;#160; The &lt;em&gt;InitiationData&lt;/em&gt; property of the SPWorkflowActivationProperties object provides XML that can be loaded into the XDocument class.&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:#ffc66d;"&gt;XDocument&lt;/span&gt; documentsXml = &lt;span style="color:#ffc66d;"&gt;XDocument&lt;/span&gt;&lt;span style="font-weight:normal;"&gt;.Load(workflowProperties.InitiationData);&lt;/span&gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt; InfoPath documents always have a &lt;a href="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/08/29/using-a-namespace-with-linq-to-xml.aspx"&gt;namespace&lt;/a&gt; defined for my so we need to use the XNamespace object for any future references inside the InfoPath form.&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:#ffc66d;"&gt;XNamespace&lt;/span&gt; documentNamespace = &lt;span style="color:#a5c25c;"&gt;&amp;quot;http://schemas.microsoft.com/office/infopath/2003/myXSD/2098-05-03T16:48:38&amp;quot;&lt;/span&gt;;&lt;/p&gt; &lt;/div&gt;   &lt;p&gt;At this point you can start using LINQ to XML to get the data you need out of the InfoPath form.&amp;#160; For example, if I had a field called &lt;em&gt;my:FirstName &lt;/em&gt;at the root of the document.&amp;#160; I could use something like the following to get the value.&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;string&lt;/span&gt; firstName = documentsXml.Root.Element(documentNamespace + &lt;span style="color:#a5c25c;"&gt;&amp;quot;FirstName&amp;quot;&lt;/span&gt;).Value;&lt;/p&gt; &lt;/div&gt;   &lt;p&gt;If you are working with other data types, you can always cast it 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;&lt;span style="color:#cc7832;"&gt;int&lt;/span&gt; customerId = &lt;span style="color:#cc7832;"&gt;int&lt;/span&gt;&lt;span style="font-weight:normal;"&gt;.Parse(documentsXml.Root.Element(documentNamespace + &lt;/span&gt;&lt;span style="color:#a5c25c;"&gt;&amp;quot;CustomerId&amp;quot;&lt;/span&gt;).Value);&lt;/p&gt; &lt;/div&gt;   &lt;p&gt;In my &lt;a href="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2009/04/29/code-samples-from-my-short-talk-about-linq-to-xml.aspx"&gt;example&lt;/a&gt; from last week, you saw how you could use LINQ to XML to iterate through a repeating table.&amp;#160; As another example to iterate through a repeating table called &lt;em&gt;Items&lt;/em&gt; with an element of &lt;em&gt;Item&lt;/em&gt;, you could do something like the following:&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;var&lt;/span&gt; items = &lt;span style="color:#cc7832;"&gt;from&lt;/span&gt; item &lt;span style="color:#cc7832;"&gt;in&lt;/span&gt; documentsXml.Root.Element(&lt;span style="color:#a5c25c;"&gt;&amp;quot;Items&amp;quot;&lt;/span&gt;).Elements(&lt;span style="color:#a5c25c;"&gt;&amp;quot;Item&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; &lt;span style="color:#cc7832;"&gt;select&lt;/span&gt; item;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&lt;span style="color:#cc7832;"&gt;foreach&lt;/span&gt; (&lt;span style="color:#cc7832;"&gt;var&lt;/span&gt; item &lt;span style="color:#cc7832;"&gt;in&lt;/span&gt; items)&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:#ffc66d;"&gt;Console&lt;/span&gt;&lt;span style="font-weight:normal;"&gt;.WriteLine(item.Element(&lt;/span&gt;&lt;span style="color:#a5c25c;"&gt;&amp;quot;Field1&amp;quot;&lt;/span&gt;).Value);&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#ffc66d;"&gt;Console&lt;/span&gt;&lt;span style="font-weight:normal;"&gt;.WriteLine(item.Element(&lt;/span&gt;&lt;span style="color:#a5c25c;"&gt;&amp;quot;Field2&amp;quot;&lt;/span&gt;).Value);&lt;/p&gt;    &lt;p style="margin:0px;"&gt;}&lt;/p&gt; &lt;/div&gt;   &lt;p&gt;In this case, I use LINQ to return an IEnumberable&amp;lt;XElement&amp;gt; representing each repeating item.&amp;#160; I can then use this in a foreach loop to print out the results.&amp;#160; Yes I know you wouldn’t use Console.Writeline in a workflow, but you get the point here.&amp;#160; LINQ to XML really offers a lot of flexibility to manipulate the XML data inside a form.&amp;#160; Hopefully, it will make your experience with InfoPath and workflow a better one.&lt;/p&gt;&lt;img src="http://dotnetmafia.com/aggbug.aspx?PostID=900" width="1" height="1"&gt;</description><category domain="http://dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/SharePoint/default.aspx">SharePoint</category><category domain="http://dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/LINQ+to+XML/default.aspx">LINQ to XML</category><category domain="http://dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/MOSS/default.aspx">MOSS</category><category domain="http://dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/InfoPath/default.aspx">InfoPath</category><category domain="http://dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/Workflow/default.aspx">Workflow</category></item><item><title>Code samples from my short talk about LINQ to XML</title><link>http://dotnetmafia.com/blogs/dotnettipoftheday/archive/2009/04/29/code-samples-from-my-short-talk-about-linq-to-xml.aspx</link><pubDate>Wed, 29 Apr 2009 15:02:00 GMT</pubDate><guid isPermaLink="false">ceb7fe2a-c56b-4d85-99e6-8dd548580538:895</guid><dc:creator>CoreyRoth</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetmafia.com/blogs/dotnettipoftheday/rsscomments.aspx?PostID=895</wfw:commentRss><comments>http://dotnetmafia.com/blogs/dotnettipoftheday/archive/2009/04/29/code-samples-from-my-short-talk-about-linq-to-xml.aspx#comments</comments><description>&lt;p&gt;Today, I had the opportunity to give a short introduction on how to do some basic queries on XML documents with LINQ to XML.&amp;nbsp; In the talk, I talked about how to create anonymous types from your XML document so that you can work with strong types.&amp;nbsp; I also showed some uses some of the classes, methods, and properties, they would use most when working with LINQ to XML.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;I also discussed how LINQ to XML can be used to make working with repeating tables in InfoPath easier.&amp;nbsp; Consider the following XML of an InfoPath form.&amp;nbsp; We have a repeating table called &lt;em&gt;RepeatingTable&lt;/em&gt; and in the table there is element called &lt;em&gt;Item&lt;/em&gt;.&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;my:myFields&lt;/span&gt; &lt;span style="COLOR:silver;"&gt;xmlns:xsi&lt;/span&gt;=&amp;quot;&lt;span style="COLOR:#a5c25c;"&gt;http://www.w3.org/2001/XMLSchema-instance&lt;/span&gt;&amp;quot;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:silver;"&gt;xmlns:my&lt;/span&gt;=&amp;quot;&lt;span style="COLOR:#a5c25c;"&gt;http://schemas.microsoft.com/office/infopath/2003/myXSD/2008-12-30T15:48:37&lt;/span&gt;&amp;quot;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:silver;"&gt;xmlns:xd&lt;/span&gt;=&amp;quot;&lt;span style="COLOR:#a5c25c;"&gt;http://schemas.microsoft.com/office/infopath/2003&lt;/span&gt;&amp;quot; &lt;span style="COLOR:silver;"&gt;xml:lang&lt;/span&gt;=&amp;quot;&lt;span style="COLOR:#a5c25c;"&gt;en-US&lt;/span&gt;&amp;quot;&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp; &amp;lt;&lt;span style="COLOR:#cc7832;"&gt;my:RepeatingTable&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;span style="COLOR:#cc7832;"&gt;my:Item&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;span style="COLOR:#cc7832;"&gt;my:Name&lt;/span&gt;&amp;gt;Polo Shirt&amp;lt;/&lt;span style="COLOR:#cc7832;"&gt;my:Name&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;span style="COLOR:#cc7832;"&gt;my:Color&lt;/span&gt;&amp;gt;26134&amp;lt;/&lt;span style="COLOR:#cc7832;"&gt;my:Color&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;span style="COLOR:#cc7832;"&gt;my:Price&lt;/span&gt;&amp;gt;19.99&amp;lt;/&lt;span style="COLOR:#cc7832;"&gt;my:Price&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;span style="COLOR:#cc7832;"&gt;my:Item&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;span style="COLOR:#cc7832;"&gt;my:Item&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;span style="COLOR:#cc7832;"&gt;my:Name&lt;/span&gt;&amp;gt;Jeans&amp;lt;/&lt;span style="COLOR:#cc7832;"&gt;my:Name&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;span style="COLOR:#cc7832;"&gt;my:Color&lt;/span&gt;&amp;gt;01823&amp;lt;/&lt;span style="COLOR:#cc7832;"&gt;my:Color&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;span style="COLOR:#cc7832;"&gt;my:Price&lt;/span&gt;&amp;gt;29.99&amp;lt;/&lt;span style="COLOR:#cc7832;"&gt;my:Price&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;span style="COLOR:#cc7832;"&gt;my:Item&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp; &amp;lt;/&lt;span style="COLOR:#cc7832;"&gt;my:RepeatingTable&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp; &amp;lt;&lt;span style="COLOR:#cc7832;"&gt;my:Id&lt;/span&gt;&amp;gt;83123&amp;lt;/&lt;span style="COLOR:#cc7832;"&gt;my:Id&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;lt;/&lt;span style="COLOR:#cc7832;"&gt;my:myFields&lt;/span&gt;&amp;gt;&lt;/p&gt;&lt;/div&gt;
&lt;p&gt;If you needed to work with this repeating table when the InfoPath form was loaded again, you could use code like the following.&amp;nbsp; You could also apply a similar technique to work with this data inside a workflow.&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:green;"&gt;// use the MainDataSource to read the XML of the InfoPath Form&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;XDocument infoPathDocument = XDocument.Load(&lt;span style="COLOR:#cc7832;"&gt;new&lt;/span&gt; System.IO.&lt;span style="COLOR:#ffc66d;"&gt;StringReader&lt;/span&gt;(MainDataSource.CreateNavigator().OuterXml));&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:green;"&gt;// required to access my: namespace&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;XNamespace myNamespace = &lt;span style="COLOR:#a5c25c;"&gt;&amp;quot;http://schemas.microsoft.com/office/infopath/2003/myXSD/2008-12-30T15:48:37&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:green;"&gt;// use descendants node to find any element with the name Item in the tree&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:#cc7832;"&gt;var&lt;/span&gt; items = &lt;span style="COLOR:#cc7832;"&gt;from&lt;/span&gt; item &lt;span style="COLOR:#cc7832;"&gt;in&lt;/span&gt; infoPathDocument.Descendants(myNamespace + &lt;span style="COLOR:#a5c25c;"&gt;&amp;quot;Item&amp;quot;&lt;/span&gt;)&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &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;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Name = item.Element(myNamespace + &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;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Color = item.Element(myNamespace + &lt;span style="COLOR:#a5c25c;"&gt;&amp;quot;Color&amp;quot;&lt;/span&gt;).Value,&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Price = item.Element(myNamespace + &lt;span style="COLOR:#a5c25c;"&gt;&amp;quot;Price&amp;quot;&lt;/span&gt;).Value&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:#cc7832;"&gt;foreach&lt;/span&gt; (&lt;span style="COLOR:#cc7832;"&gt;var&lt;/span&gt; item &lt;span style="COLOR:#cc7832;"&gt;in&lt;/span&gt; items)&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;{&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:green;"&gt;// do something&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;}&lt;/p&gt;&lt;/div&gt;
&lt;p&gt;In the code above, I use the CreateNavigator method of the MainDataSource as usual when working with an InfoPath form and I use a StringReader to load it into an XDocument.&amp;nbsp; Since InfoPath form’s always use the my namespace it is necessary to declare it using the XNamespace object and use it before referring to any element or attribute in the document.&amp;nbsp; In the example above, I create a new anonymous type with the Name, Color, and Price and then I can use a foreach loop to iterate through the items.&lt;/p&gt;
&lt;p&gt;I have attached the code for the example above as well as the other examples I mentioned in the talk to this post.&amp;nbsp; Please, let me know if you have any questions.&lt;/p&gt;
&lt;p&gt;Follow me on &lt;a href="http://twitter.com/coreyroth"&gt;twitter&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://dotnetmafia.com/aggbug.aspx?PostID=895" width="1" height="1"&gt;</description><enclosure url="http://dotnetmafia.com/blogs/dotnettipoftheday/attachment/895.ashx" length="46670" type="application/x-zip-compressed" /><category domain="http://dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/LINQ+to+XML/default.aspx">LINQ to XML</category><category domain="http://dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/Presentations/default.aspx">Presentations</category><category domain="http://dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/InfoPath/default.aspx">InfoPath</category></item><item><title>Using a namespace with LINQ to XML</title><link>http://dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/08/29/using-a-namespace-with-linq-to-xml.aspx</link><pubDate>Fri, 29 Aug 2008 20:29:12 GMT</pubDate><guid isPermaLink="false">ceb7fe2a-c56b-4d85-99e6-8dd548580538:672</guid><dc:creator>CoreyRoth</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetmafia.com/blogs/dotnettipoftheday/rsscomments.aspx?PostID=672</wfw:commentRss><comments>http://dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/08/29/using-a-namespace-with-linq-to-xml.aspx#comments</comments><description>&lt;p&gt;If you are using LINQ to XML, sooner or later, you will need to work with XML documents that don&amp;#39;t use the namespace.&amp;nbsp; For example, you might be working with the XML from an InfoPath form and you want to get the value of a particular element.&amp;nbsp; InfoPath uses the my namespace, and you can&amp;#39;t just specify it directly in the string (i.e.: &lt;em&gt;Root.Elements(&amp;quot;my:Group2&amp;quot;)&lt;/em&gt; will not work).&amp;nbsp; To resolve this, we create a &lt;em&gt;XNamespace &lt;/em&gt;object and pass it the URI to the namespace.&lt;/p&gt; &lt;div style="font-size:10pt;background:white;color:black;font-family:courier new;"&gt; &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;XNamespace&lt;/span&gt; myNamespace = &lt;span style="color:#a31515;"&gt;&amp;quot;http://schemas.microsoft.com/office/infopath/2003/myXSD/2008-02-15T09:18:32&amp;quot;&lt;/span&gt;;&lt;/p&gt;&lt;/div&gt; &lt;p&gt;Once you declare the XNamespace, it&amp;#39;s just a matter of including it before any elements or attributes you might use.&lt;/p&gt; &lt;div style="font-size:10pt;background:white;color:black;font-family:courier new;"&gt; &lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;var&lt;/span&gt; transmittalCollection = &lt;span style="color:blue;"&gt;from&lt;/span&gt; transmittalNode &lt;span style="color:blue;"&gt;in&lt;/span&gt; formDocument.Root.Descendants(myNamespace + &lt;span style="color:#a31515;"&gt;&amp;quot;group2&amp;quot;&lt;/span&gt;)&lt;/p&gt; &lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color:blue;"&gt;select&lt;/span&gt; &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Transmittal&lt;/span&gt;&lt;/p&gt; &lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt; &lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MyNode = transmittalNode.Elements(myNamespace + &lt;span style="color:#a31515;"&gt;&amp;quot;MyNode&amp;quot;&lt;/span&gt;).Any() ? transmittalNode.Element(myNamespace + &lt;span style="color:#a31515;"&gt;&amp;quot;MyNode&amp;quot;&lt;/span&gt;).Value : &lt;span style="color:blue;"&gt;null&lt;/span&gt;,&lt;/p&gt; &lt;p style="margin:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;/p&gt;&lt;/div&gt; &lt;p&gt;That&amp;#39;s all there is to it.&amp;nbsp; I have found it very useful when I want to query data out of an InfoPath form.&amp;nbsp; I just submit the data to a service and then I use LINQ to XML to parse out the values I need.&amp;nbsp; It is much easier for me than using XPath.&lt;/p&gt;&lt;img src="http://dotnetmafia.com/aggbug.aspx?PostID=672" width="1" height="1"&gt;</description><category domain="http://dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/LINQ+to+XML/default.aspx">LINQ to XML</category><category domain="http://dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/InfoPath/default.aspx">InfoPath</category></item><item><title>NEW! Web Part for Wildcard Search in Enterprise Search</title><link>http://dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/06/09/new-web-part-for-wildcard-search-in-enterprise-search.aspx</link><pubDate>Mon, 09 Jun 2008 13:28:15 GMT</pubDate><guid isPermaLink="false">ceb7fe2a-c56b-4d85-99e6-8dd548580538:603</guid><dc:creator>CoreyRoth</dc:creator><slash:comments>79</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetmafia.com/blogs/dotnettipoftheday/rsscomments.aspx?PostID=603</wfw:commentRss><comments>http://dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/06/09/new-web-part-for-wildcard-search-in-enterprise-search.aspx#comments</comments><description>&lt;p&gt;I have had &lt;a href="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/02/01/things-users-complain-about-with-enteprise-search.aspx"&gt;countless people&lt;/a&gt; tell me that they want to be able to do wildcard search in MOSS Enterprise Search using the existing Search Center site templates.&amp;nbsp; The Search Center site template uses keyword query syntax which does not support wildcards.&amp;nbsp; To get wildcards you need to use a full text SQL query.&amp;nbsp; Until now the only solutions to getting wildcard search was either use &lt;a href="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2007/08/29/product-review-ontolica-wildcard.aspx"&gt;Ontolica&amp;#39;s Wildcard Search&lt;/a&gt; or write your own search page.&amp;nbsp; Neither option has ever been appealing to me.&amp;nbsp; Ontolica replaces the entire search center and provides an extra layer of abstraction to your managed properties.&amp;nbsp; I didn&amp;#39;t want to write my own search page because the search center already produces great looking results and it would be a lot of effort to reinvent everything on the search page.&amp;nbsp; The reason why you would have had to write your own search page is that you could not get access via conventional means to the objects to change the query.&lt;/p&gt; &lt;p&gt;This is why I finally decided to take matters into my own hand.&amp;nbsp; I really just wanted to just inherit from CoreResultsWebPart, change out the keyword query with a FullTextSqlQuery and call it good.&amp;nbsp; Anyone who may have looked at this before knows it is not that simple.&amp;nbsp; The class that does all the work &lt;em&gt;SearchResultsHiddenObject&lt;/em&gt; is marked internal.&amp;nbsp;&amp;nbsp; Right now this seems an impossible task unless you bend the rules a little.&amp;nbsp; That&amp;#39;s right.&amp;nbsp; I decided I am going to break OO rules and use reflection to get to the properties I needed.&amp;nbsp; Some people say you should never do this and that its a hack.&amp;nbsp; I agree to some extent, but when you are programming against an API and the provider of said API doesn&amp;#39;t give you the tools you need to do your job, sometimes you have to bend the rules.&amp;nbsp; Let&amp;#39;s face it.&amp;nbsp; Microsoft should have given us this support out of the box and at the minimum should have allowed us to change the query that the CoreResultsWebPart executes through the API.&amp;nbsp; They did neither so here we are.&lt;/p&gt; &lt;p&gt;So how does the code work?&amp;nbsp; Well, CoreResultsWebPart happens to be the one class in all of the Enterprise Search controls that isn&amp;#39;t marked sealed.&amp;nbsp; That is good news.&amp;nbsp; Through the use of Reflector, I discovered the method I need to inherit from is &lt;em&gt;SetPropertiesOnHiddenObject&lt;/em&gt;.&amp;nbsp; Microsoft was even nice enough to mark this method as virtual for me.&amp;nbsp; I then got access to the type and then used the base type to get a FieldInfo object for the private field srho (which is the SearchResultsHiddenObject).&amp;nbsp; &lt;/p&gt; &lt;div style="font-size:10pt;background:white;color:black;font-family:courier new;"&gt; &lt;p style="margin:0px;"&gt;&lt;span style="color:green;"&gt;// get the type of the current object&lt;/span&gt;&lt;/p&gt; &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;Type&lt;/span&gt; coreResultsWebPartType = &lt;span style="color:blue;"&gt;this&lt;/span&gt;.GetType();&lt;/p&gt; &lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="margin:0px;"&gt;&lt;span style="color:green;"&gt;// get the private field containing the searchResultsHiddenObject&lt;/span&gt;&lt;/p&gt; &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;FieldInfo&lt;/span&gt; searchResultsHiddenObjectField = coreResultsWebPartType.BaseType.GetField(&lt;span style="color:#a31515;"&gt;&amp;quot;srho&amp;quot;&lt;/span&gt;, &lt;span style="color:#2b91af;"&gt;BindingFlags&lt;/span&gt;.NonPublic | &lt;span style="color:#2b91af;"&gt;BindingFlags&lt;/span&gt;.Instance);&lt;/p&gt;&lt;/div&gt; &lt;p&gt;Once, I got access to the hidden object, I read the value of the KeywordQuery property to get what the user searched for.&amp;nbsp; I then had to set this value to null, because I was replacing the keyword query with a FullTextSqlQuery.&amp;nbsp; &lt;/p&gt; &lt;div style="font-size:10pt;background:white;color:black;font-family:courier new;"&gt; &lt;p style="margin:0px;"&gt;&lt;span style="color:green;"&gt;// get the actual internal srho object attached to CoreResultsWebPart&lt;/span&gt;&lt;/p&gt; &lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;object&lt;/span&gt; searchResultsHiddenObject = searchResultsHiddenObjectField.GetValue(&lt;span style="color:blue;"&gt;this&lt;/span&gt;);&lt;/p&gt; &lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="margin:0px;"&gt;&lt;span style="color:green;"&gt;// get the type of the srho&lt;/span&gt;&lt;/p&gt; &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;Type&lt;/span&gt; searchResultsHiddenObjecType = searchResultsHiddenObject.GetType();&lt;/p&gt; &lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="margin:0px;"&gt;&lt;span style="color:green;"&gt;// get the keyword query property&lt;/span&gt;&lt;/p&gt; &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;PropertyInfo&lt;/span&gt; keywordQueryProperty = searchResultsHiddenObjecType.GetProperty(&lt;span style="color:#a31515;"&gt;&amp;quot;KeywordQuery&amp;quot;&lt;/span&gt;, &lt;span style="color:#2b91af;"&gt;BindingFlags&lt;/span&gt;.Instance | &lt;span style="color:#2b91af;"&gt;BindingFlags&lt;/span&gt;.Public);&lt;/p&gt; &lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="margin:0px;"&gt;&lt;span style="color:green;"&gt;// read what the user searched for&lt;/span&gt;&lt;/p&gt; &lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;string&lt;/span&gt; keywordQuery = (&lt;span style="color:blue;"&gt;string&lt;/span&gt;)keywordQueryProperty.GetValue(searchResultsHiddenObject, &lt;span style="color:blue;"&gt;null&lt;/span&gt;);&lt;/p&gt; &lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="margin:0px;"&gt;&lt;span style="color:green;"&gt;// set the keywordProperty to null so we can change it to a fullTextQuery&lt;/span&gt;&lt;/p&gt; &lt;p style="margin:0px;"&gt;keywordQueryProperty.SetValue(searchResultsHiddenObject, &lt;span style="color:blue;"&gt;null&lt;/span&gt;, &lt;span style="color:blue;"&gt;null&lt;/span&gt;);&lt;/p&gt;&lt;/div&gt; &lt;p&gt;It was then just a matter of forming a new SQL query string (check the code on how I did that), and setting some additional fields &lt;em&gt;_IsFullTextQuerySetFromForm&lt;/em&gt; and &lt;em&gt;m_bIsKeywordQuery&lt;/em&gt;.&amp;nbsp; &lt;/p&gt; &lt;div style="font-size:10pt;background:white;color:black;font-family:courier new;"&gt; &lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="margin:0px;"&gt;&lt;span style="color:green;"&gt;// get the fullTextQuery field&lt;/span&gt;&lt;/p&gt; &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;PropertyInfo&lt;/span&gt; fullTextQueryProperty = searchResultsHiddenObjecType.GetProperty(&lt;span style="color:#a31515;"&gt;&amp;quot;FullTextQuery&amp;quot;&lt;/span&gt;, &lt;span style="color:#2b91af;"&gt;BindingFlags&lt;/span&gt;.Instance | &lt;span style="color:#2b91af;"&gt;BindingFlags&lt;/span&gt;.Public);&lt;/p&gt; &lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="margin:0px;"&gt;&lt;span style="color:green;"&gt;// create a new query and set it&lt;/span&gt;&lt;/p&gt; &lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;string&lt;/span&gt; fullTextQueryString = GetFullTextQuery(keywordQuery, keywordsAsQuery);&lt;/p&gt; &lt;p style="margin:0px;"&gt;fullTextQueryProperty.SetValue(searchResultsHiddenObject, fullTextQueryString, &lt;span style="color:blue;"&gt;null&lt;/span&gt;);&lt;/p&gt; &lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="margin:0px;"&gt;&lt;span style="color:green;"&gt;// this field needs to be set to true to use a full text query&lt;/span&gt;&lt;/p&gt; &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;FieldInfo&lt;/span&gt; fullTextQuerySetField = searchResultsHiddenObjecType.GetField(&lt;span style="color:#a31515;"&gt;&amp;quot;_IsFullTextQuerySetFromForm&amp;quot;&lt;/span&gt;, &lt;span style="color:#2b91af;"&gt;BindingFlags&lt;/span&gt;.NonPublic | &lt;span style="color:#2b91af;"&gt;BindingFlags&lt;/span&gt;.Instance);&lt;/p&gt; &lt;p style="margin:0px;"&gt;fullTextQuerySetField.SetValue(searchResultsHiddenObject, &lt;span style="color:blue;"&gt;true&lt;/span&gt;);&lt;/p&gt; &lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="margin:0px;"&gt;&lt;span style="color:green;"&gt;// tell the srho that it is not a keyword query any more&lt;/span&gt;&lt;/p&gt; &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;FieldInfo&lt;/span&gt; isKeywordQueryField = searchResultsHiddenObjecType.GetField(&lt;span style="color:#a31515;"&gt;&amp;quot;m_bIsKeywordQuery&amp;quot;&lt;/span&gt;, &lt;span style="color:#2b91af;"&gt;BindingFlags&lt;/span&gt;.NonPublic | &lt;span style="color:#2b91af;"&gt;BindingFlags&lt;/span&gt;.Instance);&lt;/p&gt; &lt;p style="margin:0px;"&gt;isKeywordQueryField.SetValue(searchResultsHiddenObject, &lt;span style="color:blue;"&gt;false&lt;/span&gt;);&lt;/p&gt;&lt;/div&gt; &lt;p&gt;The code for this is really pretty simple (aside from the reflection).&amp;nbsp; Had the SearchResultsHiddenObject been marked public, we could have had this functionality over a year ago, but oh well.&amp;nbsp; &lt;/p&gt; &lt;h3&gt;Installation&lt;/h3&gt; &lt;p&gt;Installation is relatively simple and instructions are included in a readme file in the document.&amp;nbsp; A solution package has been provided for ease of installation.&amp;nbsp; Once the package has been installed activate the Wildcard Search Web Part feature on your site collection.&amp;nbsp; I went with a site collection feature because the search center site does not have a web part gallery in it.&amp;nbsp; Now that the feature is activated, go to your results page in your Search Center, edit the page, and add the Wildcard Search Core Results Web Part to the Bottom Zone.&amp;nbsp; You can then remove the old CoreResultsWebPart.&amp;nbsp; Also note that this web part requires .NET Framework 3.5 because I used LINQ to XML to parse through the &lt;em&gt;SelectColumns&lt;/em&gt; property.&lt;/p&gt; &lt;h3&gt;Usage&lt;/h3&gt; &lt;p&gt;Once you have the web part installed, you can perform a wildcard search by just adding an asterisk to whatever you type in the search box.&amp;nbsp; For example &lt;em&gt;app*&lt;/em&gt; would return mataches on app, apple, and application.&amp;nbsp; You can also set the &lt;em&gt;Always Use Wildcard &lt;/em&gt;property in the Miscellaneous property settings to always perform a wildcard search.&lt;/p&gt; &lt;p&gt;One thing to note.&amp;nbsp; Wildcard searches reek havoc on your search relevance.&amp;nbsp; Where you might be used to having nice clean looking results with your keyword searches, your wildcard searches will look different.&amp;nbsp; It might not always be obvious why a particular item was returned in the search results.&amp;nbsp; The best thing to do is try and see if it works for your particular situation.&lt;/p&gt; &lt;p&gt;This type of web part probably could have been sold, but I thought it was more important to give it to the community.&amp;nbsp; This feature gets asked for by MOSS customers all the time.&amp;nbsp; Finally there is an easy solution to implementing it.&amp;nbsp; Since this web part is new, I am sure there are going to be issues with it.&amp;nbsp; Please, log any issues you run into in installation or in us on the issue tracker of the CodePlex site.&amp;nbsp; Currently, it only supports simple keyword queries.&amp;nbsp; I still need to implement support for passing scopes and managed properties, so look for that in an update soon.&lt;/p&gt; &lt;p&gt;You can find the release files at &lt;a href="http://www.codeplex.com/WildcardSearch"&gt;CodePlex&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;&lt;em&gt;Corey Roth is a MOSS consultant for &lt;a href="http://www.sbti.com/"&gt;Stonebridge&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;&lt;img src="http://dotnetmafia.com/aggbug.aspx?PostID=603" width="1" height="1"&gt;</description><category domain="http://dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/SharePoint/default.aspx">SharePoint</category><category domain="http://dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/LINQ+to+XML/default.aspx">LINQ to XML</category><category domain="http://dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/MOSS/default.aspx">MOSS</category><category domain="http://dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/Enterprise+Search/default.aspx">Enterprise Search</category><category domain="http://dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/Wildcard+Search/default.aspx">Wildcard Search</category></item><item><title>How to: Check for nulls when using LINQ to XML</title><link>http://dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/02/06/how-to-check-for-nulls-when-using-linq-to-xml.aspx</link><pubDate>Wed, 06 Feb 2008 22:54:00 GMT</pubDate><guid isPermaLink="false">ceb7fe2a-c56b-4d85-99e6-8dd548580538:473</guid><dc:creator>CoreyRoth</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://dotnetmafia.com/blogs/dotnettipoftheday/rsscomments.aspx?PostID=473</wfw:commentRss><comments>http://dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/02/06/how-to-check-for-nulls-when-using-linq-to-xml.aspx#comments</comments><description>&lt;p&gt;I continue to work with LINQ to XML, and I thought this might be worth mentioning (although it is somewhat common sense).&amp;nbsp; An issue often when working with XML attribute (or elements) is that they might not always exists (i.e.: they are null).&amp;nbsp; Therefore, you need to check for this.&amp;nbsp; Specifically, this is an issue when you are assigning attributes into a new anonymous type (although it could occur using a regular type as well).&amp;nbsp; Consider the following example.&amp;nbsp; What if MyColumn is not present in some of the Item elements in the XML document?&amp;nbsp; The code would end up throwing an exception when you tried to enumerate items.&lt;/p&gt;&lt;font face="Courier New" color="blue" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt;var&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" color="black" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt; items = &lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" color="blue" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt;from&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" color="black" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt;&amp;nbsp;item &lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" color="blue" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt;in&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" color="black" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt; assetTypes.Elements(&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" color="#a31515" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt;&amp;quot;Item&amp;quot;&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" color="black" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt;)&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" color="black" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" color="black" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt;&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" color="black" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt;&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" color="black" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt;&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" color="blue" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt;select&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" color="black" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt; &lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" color="blue" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt;new&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" color="black" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt;&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" color="black" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" color="black" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/font&gt; 
&lt;p&gt;&lt;font face="Courier New" color="black" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt;&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" color="black" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt;{&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;&lt;font face="Courier New" color="black" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" color="black" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt;&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" color="black" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt;Name = item.Attribute(&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" color="#a31515" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" color="black" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt;).Value,&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" color="black" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/font&gt; 
&lt;p&gt;&lt;font face="Courier New" color="black" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt;&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" color="black" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt;&amp;nbsp;&amp;nbsp; MyColumn = &lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" color="black" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt;item.Attribute(&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" color="#a31515" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt;&amp;quot;MyColumn&amp;quot;&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" color="black" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt;).Value&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;&lt;font face="Courier New" color="black" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt;&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" color="black" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt;};&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New" size="2"&gt;&lt;span style="FONT-SIZE:10pt;"&gt;&lt;/span&gt;&lt;/font&gt;&amp;nbsp; 
&lt;p&gt;How do you fix it?&amp;nbsp; First you use the &lt;strong&gt;Any()&lt;/strong&gt; method of the attribute to see if any of that attribute exist.&amp;nbsp; Then it is just a matter of using shorthand if/then syntax.&amp;nbsp; Just replace MyColumn with the code below.&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:Courier New;"&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:blue;"&gt;&lt;font color="#000000"&gt;MyColumn = item.Attributes(&lt;/font&gt;&lt;span style="COLOR:#a31515;"&gt;&amp;quot;MyColumn&amp;quot;&lt;/span&gt;&lt;font color="#000000"&gt;).Any() ? item.Attribute(&lt;/font&gt;&lt;span style="COLOR:#a31515;"&gt;&amp;quot;MyColumn&amp;quot;&lt;/span&gt;&lt;font color="#000000"&gt;) : &lt;/font&gt;&lt;span style="COLOR:blue;"&gt;null&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;
&lt;p&gt;As you can see it is relatively simple, once you know to use Any() to look for the existance of an attribute. On a related note you can apply the same techique to see if an element exists.&lt;/p&gt;&lt;img src="http://dotnetmafia.com/aggbug.aspx?PostID=473" width="1" height="1"&gt;</description><category domain="http://dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/LINQ+to+XML/default.aspx">LINQ to XML</category></item><item><title>How I used LINQ to XML to Create Document Library Folders</title><link>http://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://dotnetmafia.com/blogs/dotnettipoftheday/rsscomments.aspx?PostID=13</wfw:commentRss><comments>http://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://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://dotnetmafia.com/aggbug.aspx?PostID=13" width="1" height="1"&gt;</description><category domain="http://dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/SharePoint/default.aspx">SharePoint</category><category domain="http://dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/LINQ+to+XML/default.aspx">LINQ to XML</category><category domain="http://dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/Document+Library/default.aspx">Document Library</category><category domain="http://dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/LINQ/default.aspx">LINQ</category></item></channel></rss>