Powered By Blogger

Monday, August 9, 2010

Create custom Article Page Layout and bind it with content type

A common requirement of many MOSS projects has been creating some news or article pages with custom columns or additional columns.To achieve this, we need to first create a custom content type with the same columns.Refer to the post create-custom-columns-and-custom content type for the same.
Here we will show how to create a custom page layout and bind it to the custom content type to create a custom article/news page.

1.Create a folder MyCustomPageLayouts under Feature folder in the project.








2.Create a Feature.xml in the MyCustomColumns folder. 
<Feature  Id="0EE75CE9-189E-4ddd-8E1D-433ACBEEA3A3"
    Title="My News Article Page Layout"
    Description="My News Article Page Layout with custom column added"
    Version="1.0.0.0"
    Scope="Site"
    Hidden="FALSE"
  ImageUrl="MySharepointProject\AfricanPith32.gif" xmlns="http://schemas.microsoft.com/sharepoint/">
  <ElementManifests>
    <ElementManifest Location="layoutfiles.xml"/>
    <ElementFile
      Location="PageLayouts\MyNewsArticle.aspx" />
  </ElementManifests>
</Feature> 
3.Create layoutfiles.xml file at the same location.
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="PageLayoutsModule" Url="_catalogs/masterpage" Path="PageLayouts" RootWebOnly="True">  //Name: name of the module,URL:where the pagelayout will be uploaded,Path:relative path in project for pagelayout file
    <File Url="MyNewsArticle.aspx" IgnoreIfAlreadyExists="TRUE" Type="GhostableInLibrary">
      <Property Name="ContentType" Value="$Resources:cmscore,contenttype_pagelayout_name;"/>
      <Property Name="PublishingPreviewImage" Value="~SiteCollection/_catalogs/masterpage/$Resources:core,Culture;/Preview Images/ArticleLinks.png, ~SiteCollection/_catalogs/masterpage/$Resources:core,Culture;/Preview Images/ArticleLinks.png"/>
      <Property Name="PublishingAssociatedContentType" Value=";#MyNews Article Page;#0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D0039F449FD09F64ed283F86980FE51CF92;#"/>
    </File>
  </Module>
</Elements>
 //Here are the properties corresponding to the above mentioned ones.
4.Create  Pagelayouts folder under MyCustomPageLayouts and put MyNewsArticle.aspx there.
Download any existing article page layout from the site and rename it to MyNewsArticle.aspx
Add the corresponding fields in the pagelayout where you want them to appear.(Use <tr><td> to place them in correct structure.) eg;
<SharePointWebControls:TextField runat="server" id="SourceText" FieldName="SourceText"/>
<SharePointWebControls:MultipleLookupField id="ArticleSubType" FieldName="ArticleSubType" runat="server" />

Add field controls here to bind custom metadata viewable and editable in edit mode only
<PublishingWebControls:editmodepanel runat="server" id="editmodepanel1">

<table cellpadding="10" cellspacing="0" align="center" class="editModePanel">
<tr><td>
<SharePointWebControls:NoteField runat="server" id="AbstractText" FieldName="AbstractText"/>
</td></tr></table>
</PublishingWebControls:editmodepanel>

Activate the feature,and you will find your pagelayout in the option when creating a new page.You can create a page with this pagelayout which will be saved in pages library as Article page with custom columns.

Sunday, August 8, 2010

Create Custom Columns and Custom Article Content Type

Here we will first create some custom columns and use them to create a custom content type.The content type will inherit sharepoint Article content type. (we will use this content type in future to create article page layout)
Creating Custom Columns
1.Create a folder MyCustomColumns under Feature folder in the project.

2.Create a Feature.xml in the MyCustomColumns folder.


<Feature
  Id="E841E6DC-4056-4515-B407-F00276AB1CCA"
  Title="MyCustomColumns"
  Description="This Feature will create My Custom Columns"
  Version="1.0.0.0"
  Scope="Site"
  Hidden="false"
  ImageUrl="MySharepointProject\AfricanPith32.gif" xmlns="http://schemas.microsoft.com/sharepoint/">
  <ElementManifests>
    <ElementManifest
      Location="MyCustomColumns.xml" />    //Provide the path of the element manifest file which will contain the columns
  </ElementManifests>
</Feature>

3.Create MyCustomColumns.xml file at the same location.

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Field ID="{56F073C7-07E3-4df3-8ADF-F186EF74EC07}"
  Name="AbstractText"
  DisplayName="Abstract Text"
  Group="Page Layout Columns"   //define a group to put the columns into it
  Type="Note" 
//Note means multiline text field
  NumLines="10"
  Required="TRUE">
  </Field>
  <Field ID="{F8C35ACB-61D4-4b6d-9FEA-DA010D704B46}"
  Name="SourceText"
  DisplayName="Source Text"
  Group="Page Layout Columns"
  Type="Text" 
//simple text field
  Required="TRUE">
  </Field>
  <Field ID="{E918A3F4-9E34-4bd2-A621-5F353B4529E2}"
    Name="ArticleSubType"
    DisplayName="Article Sub Type"
    Type="Lookup" 
//Lookup type field
        Mult="TRUE"  //True for Multi Lookup field
        Required="FALSE"
        List="F99AFCB4-4699-4AA1-BBE8-FEA3269175E6" 
//GUID of the Lookup List
        ShowField="Title"  //Field of the Lookup List which will be refered
        UnlimitedLengthInDocumentLibrary="FALSE"
        Group="Page Layout Columns"       
        />
</Elements>
Activate this feature in Site collection level and the site columns will be created under the group Page Layout Columns.
Note:
1.Need to create a Lookup List with the same GUID before activating this feature.
refer this link for creating custom list instance: create-list-instance-using-custom-list


Creating Custom Content Type
1.Create a folder MyCustomContentTypes under Feature folder in the project.
2.Feature.xml in MyCustomContentTypes folder.


<Feature  Id="1DA3D932-288E-4d10-856A-66F4DCF8D473"
  Title="MyCustomContentTypes"
  Description="This Feature will create My Custom ContentTypes"
  Version="1.0.0.0"
  Scope="Site"
  Hidden="false"
  ImageUrl="MySharepointProject\AfricanPith32.gif" xmlns="http://schemas.microsoft.com/sharepoint/">
  <ElementManifests>
    <ElementManifest
      Location="MyCustomContentTypes.xml" />  
  </ElementManifests>
  <ActivationDependencies>
    <!-- Installs the site columns feature before -->
    <ActivationDependency FeatureId="E841E6DC-4056-4515-B407-F00276AB1CCA"/>
  </ActivationDependencies>
</Feature>



3.Create MyCustomContentTypes.xml file at the same location.


<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <ContentType ID="0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D0039F449FD09F64ed283F86980FE51CF92"
Name="My News Article Page"
  Group="Page Layout Content Types"
  Description="My Custom Page Layout Content Type"
  Version="0"
  Hidden="FALSE">
    <FieldRefs>  //Refer the fields created by custom columns feature
      <FieldRef ID="{56F073C7-07E3-4df3-8ADF-F186EF74EC07}" Name="AbstractText"/>
      <FieldRef ID="{F8C35ACB-61D4-4b6d-9FEA-DA010D704B46}" Name="SourceText"/>
      <FieldRef ID="{E918A3F4-9E34-4bd2-A621-5F353B4529E2}" Name="ArticleSubType"/>
    </FieldRefs>   
  </ContentType>
</Elements>
 

Note:
ID is the most important prperty of a content type.If you are inheriting a site content type,append 00 after the GUID of the site content type and then add a new GUID to create the ID for your content type. Here
0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D
is the GUID for article content type of sharepoint publishing portal and I have added 0039F449FD09F64ed283F86980FE51CF92 to create the ID for My News Article Page Content Type.

Activate this feature in Site collection level and the My News Article Page Content Type will be created in site content types with the following fields,where our custom columns will be present along with Article Page columns.

Friday, August 6, 2010

Create a list instance using custom list template by a feature

Follow the following steps to Create a list instance using custom list template by a feature in MOSS 2007

1.Create a Feature MyCustomLists as per directory structure shown in the image.


2.Feature.xml:

<Feature Title="My Custom List Creation"
Id="A9767B45-DFA0-4077-B273-D43D35DA959E"
Description="This feature add the list template for custom list and one instance of the list"
Version="1.0.0.0"
Scope="Web"
Hidden="FALSE"
ImageUrl="MySharepointProject\AfricanPith32.gif"
xmlns="http://schemas.microsoft.com/sharepoint/">
<ActivationDependencies>
</ActivationDependencies>
<ElementManifests>
<ElementManifest Location="MyCustomListTemplate.xml" />
<ElementManifest Location="MyCustomListTemplate\MyCustomListData.xml" />
<ElementFile Location="MyCustomListTemplate\schema.xml" />
</ElementManifests>
</Feature>

3.MyCustomListTemplate.xml:

<ListTemplate
Name="MyCustomListTemplate"
BaseType="0"
Type="10111" //Should be unique number and only one list template in the site can use this type
OnQuickLaunch="TRUE"
SecurityBits="11"
Sequence="10000"
Category="Custom Lists"
DisplayName="My Custom List Template"
Description="My Custom List with image field."
Image="/_layouts/images/itgen.gif">
</ListTemplate>

4.Schema.xml: //Copy the schema.xml of CustList Feature from 12\Template feature folder and modify the following sections only.

<List xmlns:ows="Microsoft SharePoint"
Title="My Custom List"
Url="Lists/MyCustomList"
FolderCreation="FALSE"
Direction="$Resources:Direction;"
BaseType="0"
Type="10111"
Id="A9767B45-DFA0-4077-B273-D43D35DA959E" //Feature ID
xmlns="http://schemas.microsoft.com/sharepoint/">

Metadata Section: Refer Content Type

<MetaData>
<ContentTypes>
<ContentType ID="0x01"> //using the Item content type of sharepoint
<FieldRefs> //refer the custom columns which will be defined in the later section
<FieldRef ID="{a8eb573e-9e11-481a-a8c9-1104a54b2fbd}" Name="Priority" />
<FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Name="Title" Required="TRUE" />
<FieldRef ID="{D84FBE02-4859-46c3-9A75-9DFD7B848BBB}" Name="Description"/>
<FieldRef ID="{B99D5EFC-9B70-44d4-B0C0-E831ABE01916}" Name="ImageUrl"/>
</FieldRefs>
<XmlDocuments>
<XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
<FormTemplates xmlns="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">

<Display>ListForm</Display>
<Edit>ListForm</Edit>
<New>ListForm</New>
</FormTemplates>
</XmlDocument>
</XmlDocuments>
<Folder TargetName="Item" />
</ContentType>
<ContentTypeRef ID="0x0120" />
</ContentTypes>

Custom Columns Section:

<Fields>
<Field
ID="{a8eb573e-9e11-481a-a8c9-1104a54b2fbd}"
Type="Choice"
Name="Priority"
DisplayName="Priority"
SourceID="http://schemas.microsoft.com/sharepoint/v3"
StaticName="Priority">
<CHOICES>
<CHOICE>High</CHOICE>
<CHOICE>Normal</CHOICE>
<CHOICE>Low</CHOICE>
</CHOICES>
<MAPPINGS>
<MAPPING Value="1">High</MAPPING>
<MAPPING Value="2">Normal</MAPPING>
<MAPPING Value="3">Low</MAPPING>
</MAPPINGS>
<Default>Normal</Default>
</Field>
<Field ID="{D84FBE02-4859-46c3-9A75-9DFD7B848BBB}" Type="Note" Name="Description" NumLines="5" DisplayName="Description" Required="TRUE" />
<Field ID="{B99D5EFC-9B70-44d4-B0C0-E831ABE01916}" Type="URL" Name="ImageUrl" DisplayName="Image Url" Format="Image" />
</Fields>

View Fields Section: //Refer the custom columns which needs to appear in view

<ViewFields>
<FieldRef Name="LinkTitleNoMenu">
</FieldRef>
<FieldRef Name="Description" />
<FieldRef Name="ImageUrl" />
<FieldRef Name="Priority" />
</ViewFields>

4. MyCustomListData.xml: //This is the List Instance File which contains list data

<ListInstance
Id="MyCustomList"
FeatureId="A9767B45-DFA0-4077-B273-D43D35DA959E"
Title="My Custom List Instance"
Description="This is my custom list created using feature"
TemplateType="10111"
Url="Lists/MCL"> //path where the new list will be created
<Data>
<Rows>
<Row>
<Field Name="ID">1</Field>
<Field Name="Title">First</Field>
<Field Name="Description">My first custom text</Field>
<Field Name="ImageUrl">/_layouts/images/HOME.GIF</Field>
<Field Name="Priority">High</Field>
</Row>
<Row>
<Field Name="ID">2</Field>
<Field Name="Title">Second</Field>
<Field Name="Description">My second custom text</Field>
<Field Name="ImageUrl">/_layouts/images/ERROR.GIF</Field>
<Field Name="Priority">Normal</Field>
</Row>
<Row>
<Field Name="ID">3</Field>
<Field Name="Title">Third</Field>
<Field Name="Description">My third custom text</Field>
<Field Name="ImageUrl">/_layouts/images/ANNOUNCE.GIF</Field>
<Field Name="Priority">Low</Field>
</Row>
</Rows>
</Data>
 </ListInstance>
 Activate the Feature in the Site Features.
This list will be created on successful activation.




 







Possible Errors you may encounter.
1.Type ID already present for another list
2.View Fields not updated in schema,hence no item is visible.
3.Template name not same as custom list folder name.
4.Schema id not set to feature id.

Get the full code here:
http://www.mediafire.com/file/a1pthcdhn3twi88/MyCustomLists.zip