SharePoint 2010 各个常用级别对象的获取
生活随笔
收集整理的這篇文章主要介紹了
SharePoint 2010 各个常用级别对象的获取
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章主旨:獲取SharePoint 2010 中SPFarm, SPWebApplicationCollection, SPWebApplication, SPSiteCollection, SPSite, SPWebCollection, SPWeb, SPListCollection, SPList級別對象的基本操作。【更在于方便自己在工作中的記憶與學(xué)習(xí)】?
對SharePoint的基本操作有很多,要熟悉它的API不是一朝一夕就能搞定的事情,這篇文章主要是記錄如何獲取SharePoint中List級別以上對象的獲取。下面是我自己測試的代碼,希望對你有所幫助:
1 using System;2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using Microsoft.SharePoint;
6 using Microsoft.SharePoint.Administration;
7
8 namespace GetCommonLevelObject
9 {
10 class CommonLevelObject
11 {
12 //在運行這部分代碼之前要保證:
13 //1:本機(jī)已經(jīng)成功安裝SharePoint
14 //2:本項目里已經(jīng)添加了SharePoint的服務(wù)(并且命名空間添加:SharePoint和SharePoint.Administration)
15 //3:本項目的(屬性)Platform target一定是 x64 或 any cpu,(若是x86則會得不到SharePoint的對象)同時 Application的Target Framework?== .NET Framework 3.5 16 staticvoid Main(string[] args)
17 {
18 //獲取本地SharePoint的Farm(并且輸出Farm的名字)
19 SPFarm mLocalFarm = SPFarm.Local;
20 string mLocalFarmName = mLocalFarm.DisplayName;
21 Console.WriteLine("The Local SPFarm name is: {0}", mLocalFarmName);
22
23
24 //獲取WebApplicationCollection
25 SPWebApplicationCollection mWebAppCol = SPWebService.ContentService.WebApplications;
26 //遍歷WebApplicationCollection中所有的WebApp
27 foreach (SPWebApplication webapp in mWebAppCol)
28 {
29 Console.WriteLine("The name of SPWebApplication is: {0}", webapp.Name);
30 }
31 //獲取WebApplicationCollection中某個特定的WebApp
32 SPWebApplication mWebApp = mWebAppCol["SharePoint - 12345"]; //下標(biāo)為WebApp的名字或Guid
33
34
35 //獲取某個WebApp下的SiteCollection
36 SPSiteCollection mSiteCol = mWebApp.Sites;
37 //遍歷SiteCollection中所有的Site(即:手動創(chuàng)建的每一個SiteCollection)
38 foreach (SPSite site in mSiteCol)
39 {
40 Console.WriteLine("The name of SPSite is: {0}", site.Url); //site沒有Name和Title屬性
41 }
42 //獲取SiteCollection中某個特定的Site【下標(biāo)為Site的Server_Relative Url或是在SiteCollection中的序號】
?43 SPSite mSite = mSiteCol["sites/MyTeamSiteCollection1"];
44
45
46 //獲取某個Site下的WebCollection(其中包括該Site的RootWeb)
47 //(注:Web的Name是Url上顯示的名字;Title是頁面顯示的名字。手動創(chuàng)建時都可以填寫)
48 SPWebCollection mWebCol = mSite.AllWebs;
49 foreach (SPWeb web in mWebCol)
50 {
51 Console.WriteLine("The title of web is: {0}", web.Title);
52 Console.WriteLine("The name of web is: {0}", web.Name);
53 }
54
55 //獲得某個Site的RootWeb
56 SPWeb rootWeb = mSite.RootWeb;
57 //獲取某個RootWeb(Web)下的WebCollection(其中不包含該RootWeb(Web))
58 SPWebCollection mAnotherWebCol = rootWeb.Webs;
59 foreach (SPWeb web in mAnotherWebCol)
60 {
61 Console.WriteLine("The title of web is: {0}", web.Title);
62 Console.WriteLine("The name of web is: {0}", web.Name);
63 }
64
65 //獲取WebCollection中某個特定的Web
66 SPWeb mWeb = mWebCol["MyTeamSite1"];
67 //獲取某個Web下的SPListCollection(之后的以此類推)
68 //【注:Site沒有.Lists屬性,但是Web有.Lists屬性】
69 SPListCollection mListCol = mWeb.Lists;
70 foreach (SPList list in mListCol)
71 {
72 Console.WriteLine("The title of the list is: {0}", list.Title); //List沒有Name,但是有Title
73 }
74 SPList mList = mListCol["NewCustomList1"];
75
76 }
77 }
78 }
79
?
關(guān)于所有List級別以下(Item,Folder,File等等)對象的獲取以后再詳細(xì)介紹。
總結(jié)
以上是生活随笔為你收集整理的SharePoint 2010 各个常用级别对象的获取的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [Linux实用工具]Ubuntu环境下
- 下一篇: 建模算法(四)——动态规划