在某些情況下,我們需要去引用其他List中的數據,比如在網站集(Site Collection)上有個List叫Country,在其子網站(WebSite)有個List叫Employee,如果要在子Site上的Employee去引用Country中的數據,一般我們會在Site Collection上創建一個網站欄(Site Column)。這是一種解決方案。還有一種解決方案,我們也可以在項目中創建一個Lookup 類型的 Site Column,其Scope為Site,順着思路,我理所應當的創建了一個Site Column,Scope=Site,但事實上遠沒這么簡單。
其實兩種方式都是可以的,先來看第一種解決方式:
網站設置下創建網站欄
- 我以SharePoint 2013 Foundation為例,登錄SharePoint Site Collection,點擊Site settings:
- 找到Web Designer Galleries(Web設計器庫),點擊Site columns:
- 點擊創建:
- 創建Lookup(查閱項)類型的Column,並為其選擇信息來源:
接着,就可以在子站中使用該Column,同理進入子站,打開Employee List,點擊列表設置,為其添加Column:
在相應的組中找到自定義的Lookup類型的Column,點擊添加即可:
這樣就可以在跨站引用其他List中的數據了:
當然,這是一種最簡單的方法,但不妥的是需要手動去添加,而且在項目中也不能給List添加此字段,我突然想到為何不在項目中創建一個Site Column,這樣就可以批量化的去進行一些操作了,從而避免了多次需要手動添加。
在項目中創建Lookup類型的Site Column
- 創建Site Column,為了和之前的作區分,故叫"國籍2":
<?xml version="1.0" encoding="utf-8"?> <Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <Field ID="{605b3bbf-40ed-4cc7-85a3-8b6547129bf1}" Name="CountryField" StaticName="CountryField" DisplayName="國籍2" Type="Lookup" SourceID="http://schemas.microsoft.com/sharepoint/v3" List="Lists/CountryList" ShowField="LinkTitleNoMenu" Required="FALSE" Group="Custom Site Columns"> </Field> </Elements>
- 部署項目之后,找到此自定義的Site Column,發現其信息來源這兒壓根沒有設置上去:
看來要在項目中新建一個Look up類型的Site Column並非這么簡單,所以我嘗試用PowerShell導出 Employee List,查看國籍Field的SchemaXml
Add-PSSnapin "Microsoft.SharePoint.PowerShell" $site=Get-SPSite "Http://oa.kingdom.com/sites/test" $web=$site.AllWebs["testSite1"] $list=$web.Lists.TryGetList("Employee") $list|Select -ExpandProperty SchemaXml |Out-File -FilePath "C:\field.txt"
- 找到自定義的"國籍"Field:
<Field Type="Lookup" DisplayName="國籍" Required="FALSE" EnforceUniqueValues="FALSE" List="{d5907d52-99f0-49ed-85cb-f72f6e3bce4f}" WebId="0defd255-9fe9-454b-a34a-be7a86c84597" ShowField="Title" UnlimitedLengthInDocumentLibrary="TRUE" Group="自定義欄" ID="{44e07d2e-e367-479d-863e-179bdd5bd674}" SourceID="{0defd255-9fe9-454b-a34a-be7a86c84597}" StaticName="國籍" Name="_x56fd__x7c4d_" Version="1" ColName="int1" RowOrdinal="0"/>
找到不同了嗎?"國籍"和"國籍2"兩個都是Type=Lookup類型的字段,它們最大的不同是List和SourceID、WebId數據的不同,經過調試,發現List應該對應的是List 自己的ID,SourceId對應的是List所在的web Id,所以我需要動態的改變這兩個值。到底怎樣實現呢,當然是Feature 激活的時候去動態修改。
public override void FeatureActivated(SPFeatureReceiverProperties properties) { SPSite targetSite = properties.Feature.Parent as SPSite; using (SPSite site = new SPSite(targetSite.ID)) { using (SPWeb web = site.OpenWeb()) { SPField lookupField = web.Fields.TryGetFieldByStaticName("CountryField"); if (lookupField != null) { // 得到CountryField的Schema XDocument fieldSchema = XDocument.Parse(lookupField.SchemaXml); XElement root = fieldSchema.Root; if (root.Attribute("List") != null) { // 得到List對應的url string listurl = root.Attribute("List").Value; SPFolder listFolder = web.GetFolder(listurl); if (listFolder != null && listFolder.Exists) { XAttribute attrList = root.Attribute("List"); if (attrList != null) { attrList.Value = listFolder.ParentListId.ToString(); } XAttribute attrWeb = root.Attribute("SourceID"); if (attrWeb != null) { attrWeb.Value = web.ID.ToString(); } lookupField.SchemaXml = fieldSchema.ToString(); } } } } } }
- 我們到網站設置下查看以下是否已經正確設置了信息來源:
- 接着我們再去驗證下跨站是否正確,可以在子站Employee下獲取到CountryList中的數據,發現可以跨站獲取國籍2對應List中的數據,這將為我們今后在項目中可以重用這個字段提供了方便:
總結
源代碼點擊此下載。