Problem: Adding custom web parts programmatically to site in WSS 3.0. The same exception occurs when tried to add the custom web part through the onet.xml, i.e. site definition.
Microsoft.SharePoint.WebPartPages.WebPartPageUserException was unhandled
Message="Cannot recognize the XML namespace of this Web Part."
Source="Microsoft.SharePoint"
StackTrace:
at Microsoft.SharePoint.WebPartPages.WebPartImporter..ctor(SPWebPartManager manager, XmlReader reader, Uri webPartPageUri, SPWeb spWeb)
at Microsoft.SharePoint.WebPartPages.WebPartImporter.Import(SPWebPartManager manager, XmlReader reader, Boolean clearConnections, Uri webPartPageUri, SPWeb spWeb)
at Microsoft.SharePoint.WebPartPages.WebPartImporter.Import(SPWebPartManager manager, XmlReader reader, Boolean clearConnections, SPWeb spWeb)
at Microsoft.SharePoint.WebPartPages.SPWebPartManager.ImportWebPart(XmlReader reader, String& errorMessage)
at Microsoft.SharePoint.SPWebPartCollection.Add(String dwp)
at AddWebPart_Test.Program.Main(String[] args) in C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\AddWebPart_Test\AddWebPart_Test\Program.cs:line 25
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Here is the code that was used:
SPSite site = new SPSite("http://yoursite/a");
SPWeb web = site.OpenWeb();string webpartXML = "<AllUsersWebPart WebPartZoneID=\"Left\" WebPartOrder=\"1\"><![CDATA[<WebPart xmlns=\"http://schemas.microsoft.com/WebPart/v3\"><Assembly>MyCustom.WebParts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3336cbc4f402e9f7</Assembly><TypeName>MyCustom.WebParts.MyWebPart</TypeName><FrameType>None</FrameType><Title>My file</Title></WebPart>]]></AllUsersWebPart>";
SPFile file = web.GetFile("default.aspx");
SPWebPartCollection wpcol = file.GetWebPartCollection(Storage.Shared);wpcol[wpcol.Count-1].ZoneID = "Right";
wpcol[wpcol.Count-1].IsVisible = true;
wpcol[wpcol.Count-1].IsIncluded = true;
wpcol.SaveChanges(wpcol.Add(webpartXML));
Has anyone else come across this issue Any help is appreciated greatly!
Versions:
WSS 3.0
Visual Studio 2005
Cheers,
Joubin

V3 namespace is not recognized when programatically adding web part?
Super_user
I am about to release my open source project "SharePoint Tips Utility Pack" which has a code bit that imports a web part into a page. (I just need to install team explorer on my home pc, since at work the ports are blocked and I cant publish my code to codeplex).
my method is a bit different than yours. here is a snippet:
SPLimitedWebPartManager manager = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
XmlReaderSettings settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Fragment;
settings.IgnoreWhiteSpace = true;
settings.IgnoreComments = true;
XmlReader webpartDef = XmlReader.Create(pathToXml,settings); //I think you can use the xml string instead
string outText;
System.Web.UI.WebControls.WebParts.WebPart part = Manager1.ImportWebPArt(webpartDef , out outText)
string webpartZoneText = "Top";
manager.AddWebPart(part,webpartZoneText,0);
I hope this helps you. I think it was the xmlreader settings that solved that problem for me.
Jiajia
Thanks Ishai for your response... but it still didn't work for our case. I'm still getting the same exception. Here is the modified code which is basically your own.
SPLimitedWebPartManager manager = file.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
XmlReaderSettings settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Fragment;
settings.IgnoreWhitespace = true;
settings.IgnoreComments = true;
XmlReader webpartDef = XmlReader.Create(@"C:\webpart.xml", settings);
string outText;
System.Web.UI.WebControls.WebParts.WebPart part = manager.ImportWebPart(webpartDef, out outText);
string webpartZoneText = "Top";
manager.AddWebPart(part,webpartZoneText,0);
Do you have any other tricks up your sleeve
Cheers,
Joubin