SharePoint学习之一 SharePoint 2010 各个常用级别对象的获取
(转:http://www.cnblogs.com/mingmingruyuedlut/archive/2010/11/27/1889656.html) 文章主旨:获取SharePoint 2010 中SPFarm, SPWebApplicationCollection, SPWebApplication, SPSiteCollection, SPSite, SPWebCollection, SPWeb, SPListCollection, SPList级别对象的基本操作。【更在于方便自己在工作中的记忆与学习】
对SharePoint的基本操作有很多,要熟悉它的API不是一朝一夕就能搞定的事情,这篇文章主要是记录如何获取SharePoint中List级别以上对象的获取。下面是我自己测试的代码,希望对你有所帮助:
(注意:VS打开的时候一定要使用管理员身份打开,不要双击打开,不然会连不上SharePoint 如果显示SPFarm.Local为空,可参考:http://blogs.msdn.com/b/ekraus/archive/2009/11/13/sp2010-spfarm-local-is-null-or-web-application-could-not-be-found.aspx)
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:本机已经成功安装SharePoint 14 //2:本项目里已经添加了SharePoint的服务(并且命名空间添加: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"]; //下标为WebApp的名字或Guid 33 34 35 //获取某个WebApp下的SiteCollection 36 SPSiteCollection mSiteCol = mWebApp.Sites; 37 //遍历SiteCollection中所有的Site(即:手动创建的每一个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【下标为Site的Server_Relative Url或是在SiteCollection中的序号】 43 SPSite mSite = mSiteCol["sites/MyTeamSiteCollection1"]; 44 45 46 //获取某个Site下的WebCollection(其中包括该Site的RootWeb) 47 //(注:Web的Name是Url上显示的名字;Title是页面显示的名字。手动创建时都可以填写) 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 }