您的位置:首页 > 其它

SharePoint 创建 Lookup 类型的Site Column解决跨站问题

2013-10-26 15:54 471 查看

在某些情况下,我们需要去引用其他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中的数据,这将为我们今后在项目中可以重用这个字段提供了方便:



总结


源代码点击下载。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐