{"id":1050,"date":"2010-09-26T01:55:07","date_gmt":"2010-09-26T06:55:07","guid":{"rendered":"http:\/\/blogs.devhorizon.com\/reza\/?p=1050"},"modified":"2010-09-26T09:35:49","modified_gmt":"2010-09-26T14:35:49","slug":"querying-document-sets-using-spsitedataquery","status":"publish","type":"post","link":"https:\/\/blogs.devhorizon.com\/reza\/2010\/09\/26\/querying-document-sets-using-spsitedataquery\/","title":{"rendered":"Querying Document Sets Using SPSiteDataQuery"},"content":{"rendered":"<p>In SharePoint 2010, a document set is just what it is shouting at us- a bundle of documents! Agnes (SharePoint MVP) has a nice and short post <a href=\"http:\/\/dotneteers.net\/blogs\/aghy\/archive\/2009\/10\/21\/how-to-set-up-document-sets-in-sharepoint-2010.aspx\">here <\/a>about how to setup document sets. If you activate a site collection scoped feature called\u00a0 &#8220;Document ID Service&#8221;,\u00a0 document sets, like any other document in a site collection, are assigned a unique ID (i.e. &#8220;DHSD-5-14&#8221;) which can be used to retrieve them\u00a0 independent of their location in the site collection. An awesome feature of SP2010!<\/p>\n<p>A pretty common integration scenario is when a BizTalk orchestration (or CRM Workflow) needs to pass a document set id to your WCF service (installed in ISAPI folder in the WFE) and your service needs to return the document library URL based on that document set id. This way BizTalk can upload multiple documents to that document set. Let&#8217;s assume that the document library and the document set are already created and BizTalk is aware of them.<\/p>\n<p>How would you go about this?<\/p>\n<p>I haven&#8217;t found an easy way to do this through SharePoint object model .Obviously, one inefficient way to do this is to loop through all the document libraries and find the document set , like below:<\/p>\n<p>[CSharp]<br \/>\nusing (SPSite site = new SPSite(&#8220;http:\/\/foo&#8221;))<br \/>\n{<br \/>\n   using (SPWeb web = site.OpenWeb())<br \/>\n    {<\/p>\n<p>    \/\/Bad Code<br \/>\n    foreach (SPList list in web.Lists) {<br \/>\n      if (list.ContentTypesEnabled)<br \/>\n        {<br \/>\n         SPContentType spCtype = list.ContentTypes[&#8220;Document Set&#8221;];<br \/>\n         foreach (SPListItem item in list.Items)<br \/>\n          {<br \/>\n           if (item[&#8220;Document ID&#8221;].ToString().Contains(&#8220;DHSD-5-14&#8221;))<br \/>\n           string url = string.Format(&#8220;{0}\/{1}&#8221;, list.ParentWeb.Url, list.Title);<br \/>\n          }<br \/>\n        }<br \/>\n     } \/\/End of SPWeb using<br \/>\n}\/\/End of SPSite using<br \/>\n[\/CSharp]<\/p>\n<p>As you can see, when you activate Document ID Service, each document set (they are SPListIItem at the end of the day, right?), gets a new column called &#8220;Document ID&#8221; which by examining the SPListItem[&#8220;Document ID&#8221;], I am finding the one that I am looking for.<\/p>\n<p>Another (and better) way is to use the SPSiteDataQuery to query the entire SPWeb (and all the child subsites) for that document set.<\/p>\n<p>[CSharp]<\/p>\n<p>using (SPSite site = new SPSite(&#8220;http:\/\/foo&#8221;)) )<br \/>\n{<br \/>\n  using (SPWeb web = site.OpenWeb())<br \/>\n   {<br \/>\n\tSPSiteDataQuery query = new SPSiteDataQuery();<br \/>\n\tquery.Webs = &#8220;<Webs Scope=\\\"Recursive\\\" \/>&#8220;;<br \/>\n\tquery.Lists = &#8220;<Lists ServerTemplate=\\\"101\\\" \/>&#8220;;<br \/>\n\tquery.Query = &#8220;<Where><And>&#8220;;<br \/>\n\tquery.Query += &#8220;<Eq><FieldRef Name=\\\"ContentType\\\"\/><Value Type=\\\"Text\\\">Document Set<\/Value><\/Eq>&#8220;;<br \/>\n\tquery.Query += &#8220;<Eq><FieldRef Name=\\\"_dlc_DocId\\\"\/><Value Type=\\\"Text\\\">&#8221; + &#8220;DHSD-5-14&#8221; + &#8220;<\/Value><\/Eq>&#8220;;<br \/>\n\tquery.Query += &#8220;<\/And><\/Where>&#8220;;<br \/>\n\tDataTable dt = web.GetSiteData(query);<br \/>\n\tDataView dv = new DataView(dt);<br \/>\n\tif (dt.Rows.Count == 0) { \/\/ No donuts! }<br \/>\n\tDataRow dr = dt.Rows[0]; \/\/There is always one row, if any!<br \/>\n\tGuid listId = new Guid(Convert.ToString(dr[&#8220;ListID&#8221;]));<br \/>\n\tSPList targetDocLib = web.Lists[listId];<br \/>\n\tstring url = string.Format(&#8220;{0}\/{1}&#8221;, targetDocLib.ParentWeb.Url, targetDocLib.Title);<br \/>\n  } \/\/End of SPWeb using<br \/>\n} \/\/End of SPSite using<\/p>\n<p>[\/CSharp]<\/p>\n<p>If you have worked with SPSiteDataQuery before, most likely you are aware that it&#8217;s a bad ass API. A simple lowercase or or spelling mistake either results in errors or(even worse) returning no result. In my case I had issues querying document sets.<\/p>\n<p>As you can tell, I am not passing any <strong>ViewFields<\/strong> to the SPSiteDataQuery.\u00a0 These are not required as <strong>ListID<\/strong>, as well as WebID and SPListItem ID, are returned by default. You will also notice that I haven&#8217;t looped through all of the returned data rows, since if my query returns any result, there should be only one document set with the id of\u00a0 &#8220;DHSD-5-14&#8221;.<\/p>\n<p>The most important thing, thought, is the use of\u00a0<strong> _dlc_DocId <\/strong>in the query which is being referenced in the where clause of my CAML query. This is the static name for the Document ID column and\u00a0 took me a little while to figure it ou. Thankfully, using the SPSiteDataQuery class, I was able to quickly find my document set and extract the URL of its parent document library.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In SharePoint 2010, a document set is just what it is shouting at us- a bundle of documents! Agnes (SharePoint MVP) has a nice and short post here about how to setup document sets. If you activate a site collection scoped feature called\u00a0 &#8220;Document ID Service&#8221;,\u00a0 document sets, like any other document in a site [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-1050","post","type-post","status-publish","format-standard","hentry","category-sharepoint-2010"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Querying Document Sets Using SPSiteDataQuery - Reza Alirezaei&#039;s Blog %<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blogs.devhorizon.com\/reza\/2010\/09\/26\/querying-document-sets-using-spsitedataquery\/\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Reza Alirezaei\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2010\\\/09\\\/26\\\/querying-document-sets-using-spsitedataquery\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2010\\\/09\\\/26\\\/querying-document-sets-using-spsitedataquery\\\/\"},\"author\":{\"name\":\"Reza Alirezaei\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#\\\/schema\\\/person\\\/cdbb24d283697a65951cb4a14e474938\"},\"headline\":\"Querying Document Sets Using SPSiteDataQuery\",\"datePublished\":\"2010-09-26T06:55:07+00:00\",\"dateModified\":\"2010-09-26T14:35:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2010\\\/09\\\/26\\\/querying-document-sets-using-spsitedataquery\\\/\"},\"wordCount\":612,\"commentCount\":2,\"articleSection\":[\"SharePoint 2010\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2010\\\/09\\\/26\\\/querying-document-sets-using-spsitedataquery\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2010\\\/09\\\/26\\\/querying-document-sets-using-spsitedataquery\\\/\",\"url\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2010\\\/09\\\/26\\\/querying-document-sets-using-spsitedataquery\\\/\",\"name\":\"Querying Document Sets Using SPSiteDataQuery - Reza Alirezaei's Blog %\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#website\"},\"datePublished\":\"2010-09-26T06:55:07+00:00\",\"dateModified\":\"2010-09-26T14:35:49+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#\\\/schema\\\/person\\\/cdbb24d283697a65951cb4a14e474938\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2010\\\/09\\\/26\\\/querying-document-sets-using-spsitedataquery\\\/\"]}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#website\",\"url\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/\",\"name\":\"Reza Alirezaei's Blog\",\"description\":\"Blogging from the field!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#\\\/schema\\\/person\\\/cdbb24d283697a65951cb4a14e474938\",\"name\":\"Reza Alirezaei\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g\",\"caption\":\"Reza Alirezaei\"},\"url\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/author\\\/rezaa\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Querying Document Sets Using SPSiteDataQuery - Reza Alirezaei's Blog %","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blogs.devhorizon.com\/reza\/2010\/09\/26\/querying-document-sets-using-spsitedataquery\/","twitter_misc":{"Written by":"Reza Alirezaei","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blogs.devhorizon.com\/reza\/2010\/09\/26\/querying-document-sets-using-spsitedataquery\/#article","isPartOf":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/2010\/09\/26\/querying-document-sets-using-spsitedataquery\/"},"author":{"name":"Reza Alirezaei","@id":"https:\/\/blogs.devhorizon.com\/reza\/#\/schema\/person\/cdbb24d283697a65951cb4a14e474938"},"headline":"Querying Document Sets Using SPSiteDataQuery","datePublished":"2010-09-26T06:55:07+00:00","dateModified":"2010-09-26T14:35:49+00:00","mainEntityOfPage":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/2010\/09\/26\/querying-document-sets-using-spsitedataquery\/"},"wordCount":612,"commentCount":2,"articleSection":["SharePoint 2010"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blogs.devhorizon.com\/reza\/2010\/09\/26\/querying-document-sets-using-spsitedataquery\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blogs.devhorizon.com\/reza\/2010\/09\/26\/querying-document-sets-using-spsitedataquery\/","url":"https:\/\/blogs.devhorizon.com\/reza\/2010\/09\/26\/querying-document-sets-using-spsitedataquery\/","name":"Querying Document Sets Using SPSiteDataQuery - Reza Alirezaei's Blog %","isPartOf":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/#website"},"datePublished":"2010-09-26T06:55:07+00:00","dateModified":"2010-09-26T14:35:49+00:00","author":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/#\/schema\/person\/cdbb24d283697a65951cb4a14e474938"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blogs.devhorizon.com\/reza\/2010\/09\/26\/querying-document-sets-using-spsitedataquery\/"]}]},{"@type":"WebSite","@id":"https:\/\/blogs.devhorizon.com\/reza\/#website","url":"https:\/\/blogs.devhorizon.com\/reza\/","name":"Reza Alirezaei's Blog","description":"Blogging from the field!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blogs.devhorizon.com\/reza\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blogs.devhorizon.com\/reza\/#\/schema\/person\/cdbb24d283697a65951cb4a14e474938","name":"Reza Alirezaei","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g","caption":"Reza Alirezaei"},"url":"https:\/\/blogs.devhorizon.com\/reza\/author\/rezaa\/"}]}},"_links":{"self":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/posts\/1050","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/comments?post=1050"}],"version-history":[{"count":0,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/posts\/1050\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/media?parent=1050"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/categories?post=1050"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/tags?post=1050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}