Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharepoint 2010 - Adding Sample data to User Field type

Hi StackOverflow members!

I have developing custom list in Visual Studio (trough XML). I created list definition (with content type) and added list instance to it.

Here is schema.xml of my list:

<?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>

and elements.xml with list template and content type:

<?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>

The question is: how to add sample data to this list? Especially to field of type "User"?

I tried to add this code in list instance element like this:

<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>

but, error occures: Error occurred in deployment step 'Activate Features': At least one field type in not installed properly. Any help for makeing it work with dialog would be very appreciated.

like image 740
Redzio Avatar asked Dec 27 '22 15:12

Redzio


2 Answers

A User field is a special type of Lookup field. As such, the text value is represented in the format of ID;#TextValue. And really, it is only the ID that is important. The Value is largely ignored. This can be a problem because while the username will always be the same, the ID that represents that user will change from site collection to site collection.

Your row XML should look something like this:

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

You said that this is being deployed to an existing site, so you can browse to the User Information page for CONTOSO\joses. The ID will be in the URL's query string. But be aware that even if the feature works in this site, there is no guarantee that the feature will work in other site collections.

To avoid this problem, instead of seeding a list instance using XML, I will generally use a Feature Receiver to pre-populate lists with User fields. I prefer EnsureUser because it will add the user to the site if the user does not already exist.

like image 110
Rich Bennema Avatar answered Jan 09 '23 18:01

Rich Bennema


This will also work:

<Field Name="Employee">-1;#CONTOSO\joses</Field>
like image 36
Jonathan Avatar answered Jan 09 '23 18:01

Jonathan