About Me

Having 12 years experience in Microsoft technologies.Since more than 7 years working in SharePoint technologies. Expert in providing consultation for SharePoint projects. Hands on with development and administration.

Sunday 27 November 2011

Provison Exported SharePoint Content editor webpart as a feature

Applies to - WSS 3.0, MOSS 2007,SharePoint foundation, SPS 2010.

There are situations in SharePoint, where we can deploy exported content editor webpart as a feature(programatically).

1. Generally, you added some java script/jquery fucntionality in CEW and want to add the webpart in mulple pages in multiple sites.

2. Add your desired java script code & export the package as .dwp file. Don't forget to make neccessary changes like title, description, and layout properties. All changes are available in .dwp xml file.

3. The typical dwp file looks like (example)

<?xml version="1.0" encoding="utf-8"?>
<WebPart xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/WebPart/v2">
  <Title>ShowDiscussionBoardAttachments</Title>
  <FrameType>Default</FrameType>
  <Description>Allows authors to enter rich text content.</Description>
  <IsIncluded>true</IsIncluded>
  <ZoneID>Main</ZoneID>
  <PartOrder>2</PartOrder>
  <FrameState>Normal</FrameState>
  <Height />
  <Width />
  <AllowRemove>true</AllowRemove>
  <AllowZoneChange>true</AllowZoneChange>
  <AllowMinimize>true</AllowMinimize>
  <AllowConnect>true</AllowConnect>
  <AllowEdit>true</AllowEdit>
  <AllowHide>true</AllowHide>
  <IsVisible>false</IsVisible>
  <DetailLink />
  <HelpLink />
  <HelpMode>Modeless</HelpMode>
  <Dir>Default</Dir>
  <PartImageSmall />
  <MissingAssembly>Cannot import this Web Part.</MissingAssembly>
  <PartImageLarge>/_layouts/images/mscontl.gif</PartImageLarge>
  <IsIncludedFilter />
  <Assembly>Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
  <TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>
  <ContentLink xmlns="
http://schemas.microsoft.com/WebPart/v2/ContentEditor" />
  <Content xmlns="
http://schemas.microsoft.com/WebPart/v2/ContentEditor"><![CDATA[<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script><script type="text/javascript">

$(document).ready(function(){
//debugger;
//To display attachments
$('.ms-disc-padabove img[src*="attach.gif"]').each(function(index) {
var divAttachments;
var displayPageURL = $(this).parents().eq(2).next().find('a')[0].href;
if(index == 0)
{
divAttachments = "<br/><div id='divAttachments"+index+"'></div>";
$($($($(this).parents('.ms-disc-padabove')[0]).parent().next()[0]).find('div')[1]).append($(divAttachments));
}
else
{
divAttachments = "<br/><div id='divAttachments"+index+"'></div>";
$($($($(this).parents('.ms-disc-padabove')[0]).parent().next()[0]).find('div')[0]).append($(divAttachments));
}
//$($($($(this).parents('.ms-disc-padabove')[0]).parent().next()[0]).find('div > div')[0]).append($(divAttachments));
$.get(displayPageURL, function(data){
var attachmentsTable = $(data).find('#idAttachmentsTable');
$(attachmentsTable).find('a').each(function(index) {
var fileName = $(this)[0].innerHTML;
var fileExtension = fileName.substring(fileName.indexOf('.')+1).toLowerCase();
var imgTag;
if(fileExtension == 'xls' || fileExtension == 'xlsx')
{
imgTag = "<IMG BORDER=0 ALT='"+fileName +"'title='" +fileName +"'SRC='/_layouts/images/icxls.gif'>&nbsp;"+fileName;
}
else if(fileExtension == 'doc' || fileExtension == 'docx')
{
imgTag = "<IMG BORDER=0 ALT='"+fileName +"'title='" +fileName +"'SRC='/_layouts/images/icdoc.gif'>&nbsp;"+fileName;
}
else if(fileExtension == 'ppt' || fileExtension == 'pptx')
{
imgTag = "<IMG BORDER=0 ALT='"+fileName +"'title='" +fileName +"'SRC='/_layouts/images/ICPPT'>&nbsp;"+fileName;
}
else if(fileExtension == 'txt')
{
imgTag = "<IMG BORDER=0 ALT='"+fileName +"'title='" +fileName +"'SRC='/_layouts/images/ictxt.gif'>"+fileName;
}
else if(fileExtension == 'bmp')
{
imgTag = "<IMG BORDER=0 ALT='"+fileName +"'title='" +fileName +"'SRC='/_layouts/images/ICBMP.gif'>&nbsp;"+fileName;
}
else if(fileExtension == 'jpeg')
{
imgTag = "<IMG BORDER=0 ALT='"+fileName +"'title='" +fileName +"'SRC='/_layouts/images/ICJPEG.gif'>&nbsp;"+fileName;
}
else if(fileExtension == 'jpg')
{
imgTag = "<IMG BORDER=0 ALT='"+fileName +"'title='" +fileName +"'SRC='/_layouts/images/ICJPG.gif'>"+fileName;
}
else if(fileExtension == 'png')
{
imgTag = "<IMG BORDER=0 ALT='"+fileName +"'title='" +fileName +"'SRC='/_layouts/images/ICPNG.gif'>"+fileName;
}
else
{
imgTag = "<IMG BORDER=0 ALT='"+fileName +"'title='" +fileName +"'SRC='/_layouts/images/ICGEN.gif'>&nbsp;"+fileName;
}
$(this)[0].innerHTML = imgTag;
});
$("#divAttachments"+index).append(attachmentsTable[0].outerHTML);
});
})
});</script>]]></Content>
  <PartStorage xmlns="
http://schemas.microsoft.com/WebPart/v2/ContentEditor" />
</WebPart>


4. So you want to auomate the webpart to be available in site collection webpart gallery.You can do it in multiple ways like MASS update where programatically uploading the .dwp file into site collection webpart gallery.It is one time activitiy. So what if in future, if we create site collections in farm? So, here is a way to do that like, deploying .dwp file as asharepoint webpart feature.Hope you got my point.

5. Go to Visual Studio 2010 (I am using latest technology)

6. Create a solution, add SharePoint webpart component. Follwoing default files will be created
                  elements.xml
                  xxxxxxxx.cs
7. Add your desired .dwp file into the same folder. This process dowsn't requires .webpart files. Instead all settings have been saved in the form of .dwp file.

8. Instantiate your webpart by using CreateChildControl method in .cs file. This is very important step.

public class xxxxxxxx:WebPart{
protected override void CreateChildControls()
{
base.CreateChildControls();
}
}

9. Add feature folder into same solution.

10. Update the webpart related information in feature folder.

11. Build, Package  & deploy the project to desired site colelction.

12. If you use VS 2008 or lower editions, you can follow genaral MOSS 2007 wsp process to craete .wsp file. Once you are ready with .wsp, you deploy the package into desired farm.

Sample elements.xml file looks like
<?xml version="1.0" encoding="utf-8"?>Elements xmlns="http://schemas.microsoft.com/sharepoint/" >Module Name="WebParts" List="113" Url="_catalogs/wp">File Path="ShowDiscussionBoardAttachments\ShowDiscussionBoardAttachments.dwp" Url="ShowDiscussionBoardAttachments.dwp" Type="GhostableInLibrary">Property Name="Group" Value="Qualcomm WebParts" />File>Module>Elements>
Sample Feature.template.xml file looks like
<?xml version="1.0" encoding="utf-8" ?>FeatureId="AB92ADFF-5732-4c5c-b132-B90CC8B3CC32"Title="xxxx Personal Actions"Description="Feature that adds links to Welcome User section"Scope="Farm"xmlns="http://schemas.microsoft.com/sharepoint/"
</
ImageUrl ="xxxxx-logo.bmp"> Feature>
In next post, I will walk through MASS update to upload .dwp file programtically into site collection webpart gallery.
Until than, Happy programming....
<
<
<
<
<
</
</
</