none
Create List programmatically in Visual Studio using SPListCollection.Add method RRS feed

  • Question

  • Hello!

    I try to create new List in "FeatureActivated" Event Receiver. For this I use SPListCollection.Add method (String, String, String, String, Int32, String, String, SPFeatureDefinition, SPListTemplate.QuickLaunchOptions).

    My code below:

    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        using (SPWeb rootWeb = properties.Feature.Parent as SPWeb)
        {
            try
            {
                SPFeatureDefinition spfd = SPFarm.Local.FeatureDefinitions[new Guid("00BFEA71-DE22-43B2-A848-C05709900100")];
                string xmlList = "<?xml version='1.0' encoding='utf-8'?> <List xmlns:ows='Microsoft SharePoint' Title='Basic List' FolderCreation='FALSE' Direction='$Resources:Direction;' Url='Lists/Basic List' BaseType='0'> <MetaData> <ContentTypes> <ContentTypeRef ID='0x01'> <Folder TargetName='Item' /> </ContentTypeRef> <ContentTypeRef ID='0x0120' /> </ContentTypes> <Fields> </Fields> <Views> <View BaseViewID='0' Type='HTML' MobileView='TRUE' TabularView='FALSE'> <XslLink Default='TRUE'>main.xsl</XslLink> <RowLimit Paged='TRUE'>30</RowLimit> <Toolbar Position='After' Type='Freeform' /> <RowLimit>20</RowLimit> <ViewFields> <FieldRef Name='LinkTitleNoMenu'> </FieldRef> </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'> <XslLink Default='TRUE'>main.xsl</XslLink> <RowLimit Paged='TRUE'>30</RowLimit> <Toolbar Type='Standard' /> <ViewFields> <FieldRef Name='Attachments'> </FieldRef> <FieldRef Name='LinkTitle'> </FieldRef> </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> </MetaData> </List>";
                rootWeb.Lists.Add("Test1", "Test", "Lists/Test", "00BFEA71-DE22-43B2-A848-C05709900100", 100, "100", xmlList, spfd, SPListTemplate.QuickLaunchOptions.Off);
                rootWeb.Dispose();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.ToString());
            }
        }
                
    }

    xmlList is string variable of default CustomList Feature XML Schema:

    <?xml version="1.0" encoding="utf-8"?>
    <List xmlns:ows="Microsoft SharePoint" Title="Basic List" FolderCreation="FALSE" Direction="$Resources:Direction;" Url="Lists/Basic List" BaseType="0">
      <MetaData>
        <ContentTypes>
          <ContentTypeRef ID="0x01">
            <Folder TargetName="Item" />
          </ContentTypeRef>
          <ContentTypeRef ID="0x0120" />
        </ContentTypes>
        <Fields>
        </Fields>
        <Views>
          <View BaseViewID="0" Type="HTML" MobileView="TRUE" TabularView="FALSE">
            <XslLink Default="TRUE">main.xsl</XslLink>
            <RowLimit Paged="TRUE">30</RowLimit>
            <Toolbar Position="After" Type="Freeform" />
            <RowLimit>20</RowLimit>
            <ViewFields>
              <FieldRef Name="LinkTitleNoMenu">
              </FieldRef>
            </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">
            <XslLink Default="TRUE">main.xsl</XslLink>
            <RowLimit Paged="TRUE">30</RowLimit>
            <Toolbar Type="Standard" />
            <ViewFields>
              <FieldRef Name="Attachments">
              </FieldRef>
              <FieldRef Name="LinkTitle">
              </FieldRef>
            </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>
      </MetaData>
    </List>

    So, when I deploy and activate my feature I get an error:

    The system cannot find the file specified. (Exception from HRESULT: 0x80070002).

    List was created, created all content types and field, but views wasn't created. I found that I remove Url="AllItems.aspx" attribute from <View> element and Url="DispForm.aspx", Url="EditForm.aspx", Url="NewForm.aspx" from <Form> element in XML this error disappears.

    If it is important, My Feature has a Web Scope.

    Please help me figure out. Thanks!



    Friday, March 24, 2017 12:41 PM

Answers

  • WHOAAA!!!

    Simply need to use the \ as an escape character for backslash in "SetupPath" attribute in xmlList string. Like this:

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



    Friday, March 24, 2017 1:43 PM