【问题标题】:Sharepoint 2010 - Adding Sample data to User Field typeSharepoint 2010 - 将示例数据添加到用户字段类型
【发布时间】:2015-09-24 21:18:30
【问题描述】:

您好 StackOverflow 成员!

我在 Visual Studio 中开发了自定义列表(通过 XML)。我创建了列表定义(带有内容类型)并向其中添加了列表实例。

这是我列表中的 schema.xml:

<?xml version="1.0" encoding="utf-8"?>
<List xmlns:ows="Microsoft SharePoint" Title="Teams" EnableContentTypes="TRUE"  FolderCreation="FALSE" Direction="$Resources:Direction;" Url="Lists/Teams" BaseType="0" xmlns="http://schemas.microsoft.com/sharepoint/">
 <MetaData>
<ContentTypes>
  <ContentTypeRef ID="0x010089E3E6DB8C9B4B3FBB980447E313DE94" />
</ContentTypes>
<Fields>
  <Field Type="User" Name="Employee" DisplayName="Employee" Required="TRUE" ID="{7B18E941-BAAD-453A-895C-39579AB5A9F1}"  Group="Sample Group" />
  <Field Type="Boolean" Name="Manager" DisplayName="Manager" ID="{9FC927CC-45EB-4E9E-8F25-18AAEDF7DCAF}"  Group="Sample Group" />
</Fields>
<Views>
  <View BaseViewID="0" Type="HTML" MobileView="TRUE" TabularView="FALSE">
    <Toolbar Type="Standard" />
    <XslLink Default="TRUE">main.xsl</XslLink>
    <RowLimit Paged="TRUE">30</RowLimit>
    <ViewFields>      
      <FieldRef  Name="Employee" />
      <FieldRef  Name="Manager" />
    </ViewFields>
    <Query>
      <OrderBy>
        <FieldRef Name="Modified" Ascending="FALSE"></FieldRef>
      </OrderBy>
    </Query>
    <ParameterBindings>
      <ParameterBinding Name="AddNewAnnouncement" Location="Resource(wss,addnewitem)" />
      <ParameterBinding Name="NoAnnouncements" Location="Resource(wss,noXinviewofY_LIST)" />
      <ParameterBinding Name="NoAnnouncementsHowTo" Location="Resource(wss,noXinviewofY_ONET_HOME)" />
    </ParameterBindings>
  </View>
  <View BaseViewID="1" Type="HTML" WebPartZoneID="Main" DisplayName="$Resources:core,objectiv_schema_mwsidcamlidC24;" DefaultView="TRUE" MobileView="TRUE" MobileDefaultView="TRUE" SetupPath="pages\viewpage.aspx" ImageUrl="/_layouts/images/generic.png" Url="AllItems.aspx">
    <Toolbar Type="Standard" />
    <XslLink Default="TRUE">main.xsl</XslLink>
    <RowLimit Paged="TRUE">30</RowLimit>
    <ViewFields>
      <FieldRef Name="LinkTitle"></FieldRef>     
      <FieldRef  Name="Employee" />
      <FieldRef  Name="Manager" />
    </ViewFields>
    <Query>
      <OrderBy>
        <FieldRef Name="ID"></FieldRef>
      </OrderBy>
    </Query>
    <ParameterBindings>
      <ParameterBinding Name="NoAnnouncements" Location="Resource(wss,noXinviewofY_LIST)" />
      <ParameterBinding Name="NoAnnouncementsHowTo" Location="Resource(wss,noXinviewofY_DEFAULT)" />
    </ParameterBindings>
  </View>
</Views>
<Forms>
  <Form Type="DisplayForm" Url="DispForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
  <Form Type="EditForm" Url="EditForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
  <Form Type="NewForm" Url="NewForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
</Forms>

和带有列表模板和内容类型的elements.xml:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<!-- Do not change the value of the Name attribute below. If it does not match the folder name of the List Definition project item, an error will occur when the project is run. -->
<ListTemplate
    Name="TeamList"
    Type="10000"
    BaseType="0"
    OnQuickLaunch="TRUE"
    SecurityBits="11"
    Sequence="410"
    DisplayName="TeamList"
    Description="My List Definition"
    Image="/_layouts/images/itgen.png">

</ListTemplate>
<ContentType
    ID="0x010089E3E6DB8C9B4B3FBB980447E313DE94"
    Name="Team Member"
    Group="Sample Group"
    Description=""
    Version="0">
    <FieldRefs>
      <FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Name="Title" Hidden="TRUE" />
      <FieldRef ID="{7B18E941-BAAD-453A-895C-39579AB5A9F1}" />
      <FieldRef ID="{9FC927CC-45EB-4E9E-8F25-18AAEDF7DCAF}" />
    </FieldRefs>
</ContentType>

<Field Type="User" Name="Employee" DisplayName="Employee" Required="TRUE" ID="{7B18E941-BAAD-453A-895C-39579AB5A9F1}"  Group="Sample Group" />
<Field Type="Boolean" Name="Manager" DisplayName="Manager" ID="{9FC927CC-45EB-4E9E-8F25-18AAEDF7DCAF}"  Group="Sample Group" />

</Elements>

问题是:如何将示例数据添加到此列表中?尤其是“用户”类型的字段?

我尝试将此代码添加到列表实例元素中,如下所示:

<ListInstance Title="TeamList"
            OnQuickLaunch="TRUE"
            TemplateType="10000"
            Url="Lists/TeamList"
            Description="TeamList">
  <Data>
      <Rows>
          <Row>
              <Field Name="Employee">CONTOSO\joses</Field>
              <Field Name="Manager">true</Field>
          </Row>
      </Rows>
  </Data>

但是,发生错误: 部署步骤“激活功能”中发生错误:至少一个字段类型未正确安装。非常感谢任何有关使其与对话框一起使用的帮助。

【问题讨论】:

  • 在没有样本数据的情况下创建 ListInstance 是否有效?我认为您不能将“Zespół”指定为列表标题/网址,并且该错误可能具有误导性......
  • 是的。它在没有样本数据的情况下工作。对不起,误导 ListInstance 名称 - 我忘了从我的环境语言 (PL) 翻译这部分。
  • 啊,没问题。 CONTOSO\joses 是否存在?如果是,恐怕我没有更多的想法可能导致此异常。如果没有自定义代码,甚至可能无法做到这一点,即 EventReceiver
  • 是的,它存在。 SP项目是在具有传奇示例CONTOSO域组的虚拟机上开发的......

标签: sharepoint sharepoint-2010


【解决方案1】:

User 字段是一种特殊类型的查找字段。因此,文本值以ID;#TextValue 的格式表示。实际上,只有 ID 才是重要的。价值在很大程度上被忽略了。这可能是一个问题,因为虽然用户名始终相同,但代表该用户的 ID 会从网站集更改为网站集。

您的行 XML 应该如下所示:

<Data>
  <Rows>
      <Row>
          <Field Name="Employee">99;#CONTOSO\joses</Field>
          <Field Name="Manager">true</Field>
      </Row>
  </Rows>
</Data>

您说这将部署到现有站点,因此您可以浏览到 CONTOSO\joses 的用户信息页面。 ID 将在 URL 的查询字符串中。但请注意,即使该功能在本网站中有效,也不能保证该功能在其他网站集中也有效。

为了避免这个问题,我通常不会使用 XML 为列表实例播种,而是使用功能接收器来预填充包含用户字段的列表。我更喜欢EnsureUser,因为如果用户不存在,它会将用户添加到站点。

【讨论】:

    【解决方案2】:

    这也可以:

    <Field Name="Employee">-1;#CONTOSO\joses</Field>
    

    【讨论】:

    • -1作为ID有什么意义?
    【解决方案3】:

    在 SharePoint 2013 中使用 -1;# 填充用户类型的列不太容易出错。

    如果您拼写错误或提供不存在的用户:

    <Field Name="Employee">-1;#domain\nonexistent account or misspelled</Field>
    

    记录未创建。

    显式指定 ID 可能会导致分配现有组或用户,并且可能更难调试。特别是如果您还为 ID 列提供了值:

    <Field Name="ID">1</Field>
    <Field Name="Employee">1;#domain\nonexistent account or misspelled</Field>
    

    Rich 和 Jonathan 的回答都是正确的,只是想澄清一下,由于代表率低无法发表评论。

    【讨论】:

      猜你喜欢
      • 2011-03-28
      • 2011-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-29
      • 2021-06-19
      • 2015-10-18
      • 1970-01-01
      相关资源
      最近更新 更多