Wednesday, August 11, 2010

Export Webpart programmatically using SPLimitedWebpartManager

Export Webpart programmatically

==========================================================================

private void ExportWebpart(SPLimitedWebPartManager wpm, string strFilePath)
{

XmlTextWriter writer;
foreach (var webPartOnPage in wpm.WebParts)
{
try
{
System.Web.UI.WebControls.WebParts.WebPart webPartObj = (System.Web.UI.WebControls.WebParts.WebPart)(webPartOnPage);

if (!Directory.Exists(strFolderPath))
{
Directory.CreateDirectory(strFolderPath);
}
//Store below detail into xml since some web part does not export (eg: filter text web part)
// So while import we can use below detail and addwebpart using assembly name.

// RemoveSpecialCharacters(webPartObj.Title);
// wpm.GetZoneID(webPartObj);
// webPartObj.ZoneIndex;
// webPartObj.GetType().AssemblyQualifiedName;

writer = new XmlTextWriter(strFilePath, Encoding.UTF8);
wpm.ExportWebPart(webPartObj, writer);

writer.Flush();
writer.Close();
}
catch {}

}

}

==========================================================================

This above function can be use for following points:-

SPLimitedWebpartManager.ExportWebPart(Webpart, XmlWriter) throws XmlException
like below:-
The prefix '' cannot be redefined from '' to 'http://schemas.microsoft.com/WebPart/v3' within the same start element tag.
Then change xmlwriter to xmltextwriter. Same you can see in above function.

or

SPLimitedWebpartManager.ExportWebPart

The biggest issue:-
============================

SPLimitedWebpartManager.ExportWebPart(Webpart, XmlWriter) throws XmlException
XmlException when you use Text Filter Web Part (SPSlicerTextWebPart)
As per understanding, There is no solution for this. So I have other work around for this.

Why we are exporting? Since we might want import to some other site.

So what we can do, while export, store webpart detail in xml file. And while import creates 2 function as mentioned below:-

1. this is for those which have .webpart file

for this use my function "AddWebPartToPage" which u can refer from other article.

2. This is: for those which does not have webpart file means it couldn’t export. Then use following function to addwebpart

private void AddWebPartToPageUsingAssembly(string strWebPartName, SPWeb oWeb, string strZoneId, int ZoneIndex, SiteWebPart webpartSetting)
{
//here webPartSetting object contains the properties of those webpart which couldn’t export
// you can get these object using the xml which you have exported while webpart export.

SPFile file = null;
SPLimitedWebPartManager manager = null;

try
{
if (webpartSetting != null)
{
string strType = webpartSetting.Settings.AssemblyType;
Type type = Type.GetType(strType);
Assembly assemblyType = type.Assembly;
System.Web.UI.WebControls.WebParts.WebPart webPartObj = (System.Web.UI.WebControls.WebParts.WebPart)assemblyType.CreateInstance(type.FullName);
webPartObj.ChromeType = PartChromeType.None;
webPartObj.Title = webpartSetting.Settings.Title;

if (String.Compare(webPartObj.GetType().Name, "SPSlicerTextWebPart", true) == 0)
{
(((Microsoft.SharePoint.Portal.WebControls.SPSlicerBaseWebPart)(webPartObj))).FilterName = webpartSetting.Settings.FilterName;
}
file = oWeb.GetFile(strPageUrl);

if (file.CheckOutStatus == SPFile.SPCheckOutStatus.None)
{
file.CheckOut();
manager = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
manager.AddWebPart(webPartObj, strZoneId, ZoneIndex);
oWeb.Update();
}
}

}
catch (Exception ex)
{
// Exception details
}
finally
{
if (manager != null)
manager.Dispose();

if (file != null)
{
file.CheckIn("added web parts");
file.Publish("added web parts");
}
}
}
===============================================================

Thanks!
Avinash Dad

2 comments:

aksneo said...

Hi Avinash,
I came by your post just by chance. Were you in any chance working with Wipro and PfieldNet.
If yes, pls reply..

Cheers
Akshay

Avinash Maheshwari said...

Hi Akshay..

yes I was working with wipro...
I could not recognize you..

Avinash