zoukankan      html  css  js  c++  java
  • SharePoint 2010 object model 读取类容类型Document Set

    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  "Document ID Service",  document sets, like any other document in a site collection, are assigned a unique ID (i.e. "DHSD-5-14") which can be used to retrieve them  independent of their location in the site collection. An awesome feature of SP2010!

    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's assume that the document library and the document set are already created and BizTalk is aware of them.

    How would you go about this?

    I haven'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:

    using (SPSite site = new SPSite("http://foo"))
    {
       using (SPWeb web = site.OpenWeb())
        {

        //Bad Code
        foreach (SPList list in web.Lists) {
          if (list.ContentTypesEnabled)
            {
             SPContentType spCtype = list.ContentTypes["Document Set"];
             foreach (SPListItem item in list.Items)
              {
               if (item["Document ID"].ToString().Contains("DHSD-5-14"))
               string url = string.Format("{0}/{1}", list.ParentWeb.Url, list.Title);
              }
            }
         } //End of SPWeb using
    }//End of SPSite using

    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 "Document ID" which by examining the SPListItem["Document ID"], I am finding the one that I am looking for.

    Another (and better) way is to use the SPSiteDataQuery to query the entire SPWeb (and all the child subsites) for that document set.

    using (SPSite site = new SPSite("http://foo")) )
    {
      using (SPWeb web = site.OpenWeb())
       {
        SPSiteDataQuery query = new SPSiteDataQuery();
        query.Webs = "<Webs Scope=\"Recursive\" />";
        query.Lists = "<Lists ServerTemplate=\"101\" />";
        query.Query = "<Where><And>";
        query.Query += "<Eq><FieldRef Name=\"ContentType\"/><Value Type=\"Text\">Document Set</Value></Eq>";
        query.Query += "<Eq><FieldRef Name=\"_dlc_DocId\"/><Value Type=\"Text\">" + "DHSD-5-14" + "</Value></Eq>";
        query.Query += "</And></Where>";
        DataTable dt = web.GetSiteData(query);
        DataView dv = new DataView(dt);
        if (dt.Rows.Count == 0) { // No donuts! }
        DataRow dr = dt.Rows[0]; //There is always one row, if any!
        Guid listId = new Guid(Convert.ToString(dr["ListID"]));
        SPList targetDocLib = web.Lists[listId];
        string url = string.Format("{0}/{1}", targetDocLib.ParentWeb.Url, targetDocLib.Title);
      } //End of SPWeb using
    } //End of SPSite using

    If you have worked with SPSiteDataQuery before, most likely you are aware that it'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.

    As you can tell, I am not passing any ViewFields to the SPSiteDataQuery.  These are not required as ListID, as well as WebID and SPListItem ID, are returned by default. You will also notice that I haven'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  "DHSD-5-14".

    The most important thing, thought, is the use of  _dlc_DocId 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  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.

    http://aghy.hu/AghyBlog_EN/Lists/Posts/Post.aspx?ID=108

  • 相关阅读:
    [moka同学笔记]八、Yii2.0课程笔记(魏曦老师教程)[授权]
    [moka同学转载]Yii2 中国省市区三级联动
    [moka同学笔记]四、Yii2.0课程笔记(魏曦老师教程)[匿名函数的使用操作]
    [moka同学笔记]Linux命令基本格式及目录处理命令
    [moka同学笔记]使用composer 安装yii2以及遇到的问题
    [moka同学笔记]MySql语句整理
    [moka同学笔记]三、Yii2.0课程笔记(魏曦老师教程)关联字段增加搜索
    Android笔记:ListView
    Android笔记:去除标题栏
    Android笔记:内部类
  • 原文地址:https://www.cnblogs.com/ahghy/p/2617041.html
Copyright © 2011-2022 走看看