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

简介:

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

总结

源代码点击下载。

 

 

 

本博客为 木宛城主原创,基于 Creative Commons Attribution 2.5 China Mainland License发布,欢迎转载,演绎或用于商业目的,但是必须保留本文的署名 木宛城主(包含链接)。如您有任何疑问或者授权方面的协商,请给我留言。

本文转自木宛城主博客园博客,原文链接:http://www.cnblogs.com/OceanEyes/p/create-lookup-field-for-cross-site.html,如需转载请自行联系原作者
目录
打赏
0
0
0
0
20
分享
相关文章
【Azure API 管理】使用APIM进行XML内容读取时遇见的诡异错误 Expression evaluation failed. Object reference not set to an instance of an object.
【Azure API 管理】使用APIM进行XML内容读取时遇见的诡异错误 Expression evaluation failed. Object reference not set to an instance of an object.
|
10月前
|
JSTL jar包版本错误attribute items does not accept any expressions
确保你在 `items` 属性中使用了一个实际的集合或数组变量,而不是表达式,以解决这个问题。
70 0
web.xml 文件报错:cvc-id.3: A field of identity constraint ‘.....
报错内容 在对Dynamic Web Project的web.xml进行配置时,遇到如下错误,大概的翻译如下:“身份约束“ web-common-servlet-name-uniqueness”字段与元素“ web-app”匹配,但该元素没有简单的类型。”
251 0
web.xml 文件报错:cvc-id.3: A field of identity constraint ‘.....
SQL Server使用侦听器IP访问时遇到"The target principal name is incorrect. Cannot generate SSPI context"
原文:SQL Server使用侦听器IP访问时遇到"The target principal name is incorrect. Cannot generate SSPI context" 在测试SQL Server 2016 Always On时,在创建侦听器后,在客户端使用SSMS, 可以用侦...
1944 0
一个canonical标签解决site不在首页的问题
  前段时间因为竞价的同事为了方便查看转化路径,在每个关键词都设置了不同的url,具体是直接在标准网址后面加#和参数,例如www.abc.com/#defgh,www.abc.com/#ijklmn,因为他设置的着陆页很多是首页,所以很容易出现问题,转化率低不说,还可能造成大量的重复页面,去百度site一下,www.
760 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等