#副標= Web Part程式開發系列(4)
#大標=透過SharePoint存取網站內容
#作者=文/林賢達
-----box-----
#程式1 取出所有的虛擬伺服器。
using Microsoft.SharePoint.Administration;
protected DropDownList ddlVirServers;
protected DropDownList ddlSites,ddlWebs;
private SPGlobalAdmin myGlobalAdm;
private SPSiteCollection mySites;
private SPWebCollection myWebs;
myGlobalAdm=new SPGlobalAdmin();
SPVirtualServerCollection myVirServers;
myVirServers=myGlobalAdm.VirtualServers;
string serverDesc,serverUrl;
foreach (SPVirtualServer server in myVirServers){
serverDesc=server.Description;
serverUrl=server.Url.ToString();
ddlVirServers.Items.Add(
new ListItem(serverDesc,serverUrl));
-----end-----
-----box-----
#程式2 取出網站群集的頂層網站。
string virServerUrl;
virServerUrl=ddlVirServers.SelectedItem.Value;
myGlobalAdm=new SPGlobalAdmin();
SPVirtualServer myVirServer;
myVirServer= myGlobalAdm.OpenVirtualServer (new
Uri(virServerUrl));
mySites=myVirServer.Sites;
string siteUrl;
foreach (SPSite site in mySites){
siteUrl=site.Url.ToString();
ddlSites.Items.Add(new ListItem(siteUrl));
}
-----end-----
-----box-----
#程式3 取出網站群集中的所有網站。
string siteUrl=ddlSites.SelectedItem.Value;
SPSite mySite=new SPSite(siteUrl);
myWebs =mySite.AllWebs;
foreach (SPWeb web in myWebs){
ddlWebs.Items.Add(new ListItem(web.Name));
}
-----end-----
-----box-----
#程式4
SPSite mySite;
mySite=new SPControl.GetContextSite(Context);
SPWeb myWeb=mySite.OpenWeb();
//或寫成
SPWeb myWeb;
myWeb=new SPControl.GetContextWeb(Context);
-----end-----
-----box-----
#程式5
SPSite mySite=new SPSite(
"http://server_name:port_number/site_name");
SPWeb myWeb=mySite.OpenWeb();
//或寫成
SPSite mySite=new SPSite (
"http://server_name:port_number");
SPWeb myWeb=mySite.AllWebs["site_name"];
-----end-----
-----box-----
#程式6
SPWebCollection myWebs=mySite.AllWebs;
Foreach (SPWeb web in myWebs){
// to do something
}
-----end-----
-----box-----
#程式7
using Microsoft.SharePoint.WebControls;
int idx=0;
SPWeb myWeb=SPControl.GetContextWeb(Context);
SPListCollection myLists=myWeb.Lists;
foreach (SPList list in myLists){
output.Write(Convert.ToString(++idx) + ". ");
output.Write(list.Title + " (" +
list.BaseType.ToString() + ")");
output.Write("
");
}
-----end-----
-----box-----
#程式8
SPWeb myWeb=SPControl.GetContextWeb(Context);
Guid myListGuid=myWeb.Lists.Add("MyLibary",
"IT Resource Library",
SPListTemplateType.DocumentLibrary);
-----end-----
-----box-----
#程式9
SPList myList=myWeb.Lists[myListGuid];
-----end-----
-----box-----
#程式10
SPListCollection myLists
myLists =myWeb.GetListsOfType
(SPBaseType.DocumentLibrary);
-----end-----
-----box-----
#程式11
myWeb.Lists.Delete(myListGuid);
-----end-----
-----box-----
#程式12 新增一個基本類型的欄位。
myLists.Fields.Add("Memo", SPFieldType.Text, false);
myLists.Fields.Add("Num", SPFieldType.Number, true);
-----end-----
-----box-----
#程式13新增一個複雜欄位。
myLists.Fields.AddLookup("Annnouce",
MyLists["Announcements"].ID ,true);
SPFieldLookup myLookup;
myLookup=(SPField)myList.Fields["Annnouce"];
myLookup.LookupField="Title";
-----end-----
-----box-----
#程式14
int idx=0;
SPWeb myWeb=SPControl.GetContextWeb(Context);
SPList myList=myWeb.Lists["Document Library"];
foreach (SPField field in myList.Fields)
{
output.Write(Convert.ToString(++idx) + ". ");
output.Write(field.Title + " (" +
field.TypeAsString + ")");
output.Write("
");
}
-----end-----
-----box-----
#程式15
string linkUrl,linkTitle,linkNote;
string[] aUrl;
SPListItem newLink;
SPWeb myWeb=SPControl.GetContextWeb(Context);
SPList myLinks=myWeb.Lists["Links"];
if (myLinks.Items.Count>0){
SPList myBackupLinks;
myBackupLinks=myWeb.Lists["BackupLinks"];
foreach (SPListItem item in myLinks.Items)
{ //read from source
aUrl=item["URL"].ToString().Split(',');
linkUrl=aUrl[0];
linkTitle=aUrl[1];
linkNote=item["Notes"].ToString();
//add to destination
newLink=myBackupLinks.Items.Add();
newLink["URL"]=linkUrl + "," + linkTitle;
newLink["Notes"]=linkNote;
newLink.Update();
}
myBackupLinks.Update();
//delete source
for(int idx=0;idx!=myLinks.Items.Count;idx++)
myLinks.Items.Delete(idx);
}
-----end-----
-----box-----
#程式16
SPQuery myQuery = new SPQuery();
myQuery.Query = "" + "" +
"" +
"" +
"" +
"" +
" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"";
-----end-----
-----box-----
#程式17
SPListItemCollection myItems;
myItems = myList.GetItems(myQuery);
foreach (SPListItem item in myItems){
//to do something
}
-----end-----
-----box-----
#程式18
//建立要顯示的一組欄位
using System.Collections.Specialized;
StringCollection vwFields = new StringCollection();
string [] aryCols = {"Title", "URL", "Content",
"Created", "Created By"};
vwFields.AddRange(aryCols);
//建立查詢條件及排序欄位
string vwQuery =
"" +
"" +
"" +
"" +
"" +
"Administrator" +
""+
"";
-----end-----
-----box-----
#程式19
myList.Views.Add("MyCustomView", vwFields,
vwQuery, 100 , true, true,
SPViewCollection.SPViewType.Html, false);
-----end-----