Saturday, May 12, 2012

How to display custom pages for different content types in SP list

I got requirement like ,when creating content types in list , custom design pages for every content type has to be displayed for the items in list.
               Recently in my migration from lotus notes to SharePoint 2010 , client need this type of requirement .So after browsing long time i got a link saying how to create it.
     Here is the link, we achieved the above requirement.  
     http://sprider.org/2012/03/06/sharepoint-custom-list-with-custom-display-forms-for-each-content-types/ 
     I  created custom pages and assigned to each content type in list for display form as mentioned in above link.It was success and working as expected.
                   After few days we faced new issue on following above approach i.e., in search functionality of SharePoint , custom pages designed were not appearing showing only default display form of the list.
So with the client object model of SP2010 fixed this issue as below.
  1. Create the custom pages for all content types 
  2. Do not assign to each content type as shown in above link
  3. Open the default display form of list and insert following  code as below
/* Getting query string for item id*/
     JSRequest.EnsureSetup(); 
     var itemId = JSRequest.QueryString["ID"];
/* Client object model code for retrieving item properties*/
      ExecuteOrDelayUntilScriptLoaded(GetItems, "sp.js");               
       var context;   
       var web = null;   
       var collListItem;
       var Initialvalue = 1;
       var docItem;
     function GetItems()
    {
    try
    {
    context = new SP.ClientContext.get_current();
    web = context.get_web();
    var lists = web.get_lists();
    var doclogList = lists.getByTitle("listname");
    docItem = doclogList.getItemById(itemId);
    context.load(docItem,'Title', 'ContentTypeId');
   context.executeQueryAsync(Function.createDelegate(this, this.itemReceived), Function.createDelegate(this, this.failed));
   }
   catch(e)
   {
   alert(e);
   }
}
/*  Content type ids for each content type in SP list - you can get id from your site after creating content types.
Frmdebookv2=0x0100863B8B29335BAF4481A5CA2ACCED155E008C0AEC34BA9F1447913B0E28247BD0BA

Mainv2 =0x0100B5DA9AD10641494AA721F44CCF89A928003A57D431589C80428EBB37AA055E9CF3

fmDebook= 0x010400A729D5FD3202854C882E8D8CBBB6FA5700997CCC7B718AC442B1112497F41FD08

Main =0x010400E39B41873A1F7840B1FE133CE431E56C00DA02B82C4CC08444A1E64B47676551F4 */

/* Function to redirect to respective custom based on the content types */
function itemReceived() 
{  
    var cTitle=docItem.get_item('ContentTypeId');
    var itemIdval = JSRequest.QueryString["ID"];

 if(cTitle=='0x0100B5DA9AD10641494AA721F44CCF89A928003A57D431589C80428EBB37AA055E9CF3')
    {
/* Redirect to Custom content type page designed for Mainv2 content type */
    window.location.href='/sites/booklist/Lists/Checklist%20Database/AGSMainv2Dispform.aspx?ID='+itemIdval;
    }
else if(cTitle=='0x010400E39B41873A1F7840B1FE133CE431E56C00DA02B82C4CC08444A1E64B47676551F4')
    {
/* Redirect to Custom content type page designed for Main content type */                   window.location.href='/sites/booklist/Lists/Checklist%20Database/DispForm.aspx?ID='+itemIdval;
    }    
else if(cTitle=='0x0100863B8B29335BAF4481A5CA2ACCED155E008C0AEC34BA9F1447913B0E28247BD0BA')
    {
/* Redirect to Custom content type page designed for FrmDebookv2 content type */ 
    window.location.href='/sites/booklist/Lists/Checklist%20Database/AGSFrmDebookV2AGSDispform.aspx?ID='+itemIdval;
    }
elseif(cTitle=='0x010400A729D5FD3202854C882E8D8CBBB6FA5700997CCC7B718AC442B1112497F41FD08')     {
/* Redirect to Custom content type page designed for FrmDebook content type */ 
    window.location.href='/sites/booklist/Lists/Checklist%20Database/AGSfmDebookDispform.aspx?ID='+itemIdval;
    }    
}
function failed(sender, args) {
    alert('failed. Message:' + args.get_message());

No comments:

Post a Comment