使用客户端对象模型获取SharePoint Server的所有信息

使用客户端对象模型获取SharePoint Server的所有信息,sharepoint,sharepoint-2010,Sharepoint,Sharepoint 2010,我想创建一个Windows应用程序 显示: 所有Web应用程序 每个web应用程序的站点集合 每个网站集的网站 每个站点的子站点 在树状视图中列出每个站点和子站点的所有库 在这里,我不想给出任何静态URL,在应用程序启动时,如果SharePoint安装在该计算机上,所有信息将自动填充到树状视图中 这可能吗?如果是,则如何执行?我的代码: protected void Page_Load(object sender, EventArgs e) { if

我想创建一个Windows应用程序

显示:

  • 所有Web应用程序

  • 每个web应用程序的站点集合

  • 每个网站集的网站

  • 每个站点的子站点

  • 在树状视图中列出每个站点和子站点的所有库

在这里,我不想给出任何静态URL,在应用程序启动时,如果SharePoint安装在该计算机上,所有信息将自动填充到树状视图中

这可能吗?如果是,则如何执行?

我的代码:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var service = SPFarm.Local.Services.GetValue<SPWebService>(string.Empty);
                foreach (SPWebApplication webApplication in service.WebApplications)
                {
                    ShowSiteCollection(webApplication.Sites);
                }
            }
        }

private void ShowSiteCollection(IEnumerable<SPSite> sites)
        {
            foreach (SPSite site in sites)
            {
                using (site)
                {
                    var rootWeb = site.RootWeb;
                    var node = new TreeNode(rootWeb.Title, rootWeb.Url);
                    if (rootWeb.Webs.Count > 0)
                    {
                        ShowWebCollection(rootWeb.Webs, (title, url) => node.ChildNodes.Add(new TreeNode(title, url)));
                    }

                    siteCollectionTree.Nodes.Add(node);
                }
            }
        }

 private static void ShowWebCollection(SPWebCollection collection, Action<string, string> func)
        {
            for (var i = 0; i < collection.Count; i++)
            {
                var info = collection.WebsInfo[i];

                func.Invoke(info.Title, info.ServerRelativeUrl);

                if (collection[i].Webs.Count > 0)
                {
                    ShowWebCollection(collection[i].Webs, func);
                }
            }
        }
受保护的无效页面加载(对象发送方,事件参数e)
{
如果(!IsPostBack)
{
var service=SPFarm.Local.Services.GetValue(string.Empty);
foreach(SPWebApplication-webApplication-in-service.WebApplications)
{
ShowSiteCollection(webApplication.Sites);
}
}
}
私有void ShowSiteCollection(IEnumerable站点)
{
foreach(站点中的SPSite站点)
{
使用(站点)
{
var rootWeb=site.rootWeb;
var node=newtreenode(rootWeb.Title,rootWeb.Url);
如果(rootWeb.Webs.Count>0)
{
ShowWebCollection(rootWeb.Webs,(title,url)=>node.ChildNodes.Add(newtreenode(title,url));
}
siteCollectionTree.Nodes.Add(节点);
}
}
}
私有静态无效ShowWebCollection(SPWebCollection集合,操作函数)
{
对于(var i=0;i0)
{
ShowWebCollection(集合[i].Webs,func);
}
}
}
以及获取列表的方法:

private static IEnumerable<List> GetSiteLists(string siteUrl)
        {
            using (var context = new ClientContext(siteUrl))
            {
                var query = from lists in context.Web.Lists
                            where !lists.Hidden
                            select lists;
                var siteLists = context.LoadQuery(query);
                context.ExecuteQuery();

                return siteLists;
            }
        }
私有静态IEnumerable GetSiteList(字符串siteUrl)
{
使用(var context=newclientcontext(siteUrl))
{
var query=来自context.Web.lists中的列表
哪里!列表。隐藏
选择列表;
var siteLists=context.LoadQuery(查询);
context.ExecuteQuery();
返回网站列表;
}
}

我不认为这个解决方案是最好的,但它是有效的。祝你好运

.Net托管客户端对象模型:获取所有网站、库和项目

在SharePoint 2010中,我们有3个客户端对象模型

  • .Net托管客户端对象模型
  • 银光
  • ECMA脚本/JavaScript
  • 今天,我将介绍.NET托管客户端对象模型,用于从SharePoint检索所有网站、库和项目

    桌面应用程序的先决条件参考:

    一,。Microsoft.SharePoint.Client.dll
    2.Microsoft.SharePoint.Client.Runtime.dll

    让我的自定义类感觉我们正在处理SharePoint Server对象模型:

    public class SPWeb
     {
         public string WebGUID { get; set; }
         public string Title { get; set; }
         public string ServerRelativeUrl { get; set; }
         public string ParentType { get; set; }
         public SPBase Parent { get; set; }
     }
     public class SPList
     {
         public string ListGUID { get; set; }
         public string Title { get; set; }
         public string ParentWebUrl { get; set; }
         public string RootFolderServerRelativeUrl { get; set; }
     }
    
    public class SPFolder
     {
         public string ID { get; set; }
         public string UniqueID { get; set; }
         public string Name { get; set; }
         public string Title { get; set; }
         public string ParentWebUrl { get; set; }
         public string ListName { get; set; }
         public string ServerRelativeUrl { get; set; }
         public string ParentFolderServerRelativeUrl { get; set; }
     }
    
     public class SPListItem
     {
         public string ID { get; set; }
         public string Name { get; set; }
         public string Title { get; set; }
         public string ServerRelativeUrl { get; set; }
         public string Modified { get; set; }
         public string ModifiedBy { get; set; }
         public string CreatedBy { get; set; }
         public string Size { get; set; }
         public string Created { get; set; }
         public string UniqueId { get; set; }
         public string ListName { get; set; }
     }
    
    用于获取网站/库/项目的方法:

    public List<SPWeb> GetAllWebs(string webURL)
    {
        var webColl = new List<SPWeb>();
        try
        {
        var currentWeb = _ctx.Site.OpenWeb(webURL);
        var allWebs = currentWeb.Webs;
        var webCollection = _ctx.LoadQuery(allWebs.Include(web => web.Title,
        web => web.Id, web => web.ServerRelativeUrl));
        _ctx.ExecuteQuery();
        webColl.AddRange(webCollection.Select(web => new SPWeb
                                    {
                                      Title = web.Title,
                                      WebGUID = web.Id.ToString(),
                                      ServerRelativeUrl = web.ServerRelativeUrl
                                     }));
        }
        catch (Exception ex)
        {
          // error log
        }
     return webColl;
    }
    
    public List<SPList> GetAllLibraries(string webURL)
    {
       var listColl = new List<SPList>();
       try
       {
         var currentWeb = _ctx.Site.OpenWeb(webURL);
         var query = from list in currentWeb.Lists
                     where list.BaseType == BaseType.DocumentLibrary
                     select list;
         var AllLists = currentWeb.Lists;
         var listCollection = _ctx.LoadQuery(query.Include(myList => myList.Title,
                                       myList => myList.Id,
                                       myList => myList.RootFolder.ServerRelativeUrl,
                                       myList => myList.ParentWebUrl,
                                       myList => myList.Hidden,
                                       myList => myList.IsApplicationList));
      _ctx.ExecuteQuery();
    
      listColl.AddRange(from list in listCollection
                        where !list.Hidden
                        select new SPList
                        {
                            Title = list.Title,
                            ListGUID = list.Id.ToString(),
                            RootFolderServerRelativeUrl = list.RootFolder.ServerRelativeUrl,
                            ParentWebUrl = list.ParentWebUrl
                         });
       }
       catch (Exception ex)
       {
           // error log
       }
      return listColl;
    }
    
    public List<SPFolder> GetAllFolder(string webURL, string listName, string folderName)
    {
        var itemColl = new List<SPFolder>();
        try
        {
          var currentWeb = _ctx.Site.OpenWeb(webURL);
          var currentList = currentWeb.Lists.GetByTitle(listName);
    
          var query = new CamlQuery();
    
          if (folderName.Length > 0)
             query.FolderServerRelativeUrl = folderName;
    
          query.ViewXml = @"<View><Query><Where>
                            <Or>
                              <Eq>
                                 <FieldRef Name='ContentType' />
                                 <Value Type='Text'>Document Set</Value>
                              </Eq>
                              <Eq>
                                 <FieldRef Name='ContentType' />
                                 <Value Type='Text'>Folder</Value>
                              </Eq>
                            </Or>
                            </Where></Query></View>";
    
          var listitems = currentList.GetItems(query);
      _ctx.Load(listitems);
      _ctx.ExecuteQuery();
    
      itemColl.AddRange(listitems.ToList().Select(item => new SPFolder()
                       {
                           ID = Convert.ToString(item["ID"]),
                           UniqueID = Convert.ToString(item["GUID"]),
                           ListName = listName,
                           ParentWebUrl = webURL,
                           Title = Convert.ToString(item["FileLeafRef"]),
                           Name = "folder",
                           ServerRelativeUrl = Convert.ToString(item["FileRef"])
                       }).AsEnumerable());
      }
      catch (Exception ex)
      {
         // error log
      }
      return itemColl;
    }
    public List<SPListItem> GetAllItems(string webURL, string listName, string folderName)
    {
        var itemColl = new List<SPListItem>();
        try
        {
            var currentWeb = _ctx.Site.OpenWeb(webURL);
            var currentList = currentWeb.Lists.GetByTitle(listName);
            var query = new CamlQuery();
    
            if (folderName.Length > 0)
               query.FolderServerRelativeUrl = folderName;
    
            var myquery = from myitems in currentList.GetItems(query)
                          select myitems;
    
            var listitems = _ctx.LoadQuery(myquery.Include(myitem => myitem["ID"],
                                          myitem => myitem["FileLeafRef"],
                                          myitem => myitem["Modified"],
                                          myitem => myitem["File_x0020_Size"],
                                          myitem => myitem["Modified_x0020_By"],
                                          myitem => myitem["Created_x0020_By"],
                                          myitem => myitem["FileRef"],
                                          myitem => myitem["UniqueId"],
                                          ));
    
             _ctx.ExecuteQuery();
    
             foreach (var nitem in listitems.Select(item => new SPListItem
             {
                   ID = Convert.ToString(item["ID"]),
                   ParentWebUrl = webURL,
                   Title = Convert.ToString(item["FileLeafRef"]),
                   Modified = item["Modified"] != null ?         Convert.ToString(item["Modified"]) : string.Empty, Size = item["File_x0020_Size"] != null ? Convert.ToString(item["File_x0020_Size"]) : string.Empty,
               CreatedBy = item["Created_x0020_By"] != null ? Convert.ToString(item["Created_x0020_By"]) : string.Empty,
               ModifiedBy = item["Modified_x0020_By"] != null ? Convert.ToString(item["Modified_x0020_By"]) : string.Empty,
               UniqueId = item["UniqueId"].ToString(),
               ServerRelativeUrl = Convert.ToString(item["FileRef"]),
               ListName = listName
         }))
    
         itemColl.Add(nitem);
    }
    catch (Exception ex)
    {
         // error log
    }
    return itemColl;
    }
    
    公共列表GetAllWebs(字符串webURL)
    {
    var webColl=新列表();
    尝试
    {
    var currentWeb=_ctx.Site.OpenWeb(webURL);
    var allWebs=currentWeb.Webs;
    var webCollection=\u ctx.LoadQuery(所有web.Include(web=>web.Title,
    web=>web.Id,web=>web.ServerRelativeUrl);
    _ctx.ExecuteQuery();
    webColl.AddRange(webCollection.Select(web=>newspweb
    {
    Title=web.Title,
    WebGUID=web.Id.ToString(),
    ServerRelativeUrl=web.ServerRelativeUrl
    }));
    }
    捕获(例外情况除外)
    {
    //错误日志
    }
    返回webColl;
    }
    公共列表GetAllLibraries(字符串webURL)
    {
    var listColl=新列表();
    尝试
    {
    var currentWeb=_ctx.Site.OpenWeb(webURL);
    var query=来自currentWeb.Lists中的列表
    其中list.BaseType==BaseType.DocumentLibrary
    选择列表;
    var AllLists=currentWeb.Lists;
    var listCollection=\u ctx.LoadQuery(query.Include(myList=>myList.Title,
    myList=>myList.Id,
    myList=>myList.RootFolder.ServerRelativeUrl,
    myList=>myList.ParentWebUrl,
    myList=>myList.Hidden,
    myList=>myList.IsApplicationList));
    _ctx.ExecuteQuery();
    listColl.AddRange(来自listCollection中的列表
    哪里!列表。隐藏
    选择新建SPList
    {
    Title=list.Title,
    ListGUID=list.Id.ToString(),
    RootFolderServerRelativeUrl=list.RootFolder.ServerRelativeUrl,
    ParentWebUrl=list.ParentWebUrl
    });
    }
    捕获(例外情况除外)
    {
    //错误日志
    }
    返回listColl;
    }
    公共列表GetAllFolder(字符串webURL、字符串listName、字符串folderName)
    {
    var itemColl=新列表();
    尝试
    {
    var currentWeb=_ctx.Site.OpenWeb(webURL);
    var currentList=currentWeb.Lists.GetByTitle(listName);
    var query=new CamlQuery();
    如果(folderName.Length>0)
    query.FolderServerRelativeUrl=folderName;
    query.ViewXml=@”
    文件集
    文件夹