Katu Dhara

Hi all,

I am facing problem while adding a custom webpart in a sharepoint2007 web page.

The code:

using System.Xml.Serialization;

using System.Xml;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Utilities;

using Microsoft.SharePoint.WebControls;

using Microsoft.SharePoint.WebPartPages;

namespace AddWebPartProgrammatically

{

[DefaultProperty("Text")]

[XmlRoot(Namespace="AddWebPartProgrammatically")]

[ToolboxData("<{0}:AddWebPart runat=server></{0}:AddWebPart>")]

public class AddWebPart : WebPart

{

[Bindable(true)]

[Category("Appearance")]

[DefaultValue("")]

[Localizable(true)]

string msg;

protected override void CreateChildControls()

{

try

{

SPWeb myWeb = SPControl.GetContextWeb(Context);

SPLimitedWebPartManager coll = myWeb.GetLimitedWebPartManager("default.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

//SPLimitedWebPartCollection allwebPart = coll.WebParts;

string errorMsg;

string dwp = @"< xml version=""1.0"" ><WebPart xmlns=""http://schemas.microsoft.com/WebPart/v2""><Assembly>DisplayTitleOfList, Version=1.0.0.0, Culture=Neutral </Assembly><TypeName>DisplayTitleOfList.DisplayTitleWebPart</TypeName><Title>My Simple Web Part</Title><Description>A sample Web Part</Description></WebPart>";

XmlTextReader reader = new XmlTextReader(dwp);

coll.ImportWebPart(reader, out errorMsg);

}

catch (Exception ex)

{

msg = ex.Message;

}

}

protected override void RenderContents(HtmlTextWriter output)

{

RenderChildren(output);

output.Write(msg);

}

}

I am getting an error while reading the dwp string by xmlReader.

ex.Message = "Illegal characters in path."

Please help me.

Thanks

KA2




Re: SharePoint - Development and Programming Facing problem while adding web part in Sharepoint2007 using SPLimitedWebPartManager

faf38

Hi Katu,
AFAIK you can't create an instance of a XMLTextReader class through passing a xml file content to ctor - there is no such ctor as you can see on msdn. However you can add web part in programmatic way passing to ctor e.g. path to the dwp file.
HTH,
Regards,
Lukasz




Re: SharePoint - Development and Programming Facing problem while adding web part in Sharepoint2007 using SPLimitedWebPartManager

Katu Dhara

Hi Lukasz,

Thanks for reply.I have modified the code but still giving error of deserializing.




Re: SharePoint - Development and Programming Facing problem while adding web part in Sharepoint2007 using SPLimitedWebPartManager

Kumarakuru

DOnt use the Files to do this kind of operations, especially when you are in the internet. Always look for streams.

I have attached a sample piece of code below which contains

  1. Stream instead of a file
  2. Load the control to the page
  3. Provide a easy way of supplying (dont hard code) the DWP content by using a custom property

Happy programming

______________________________________

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.SharePoint.WebPartPages;

using System.Xml.Serialization;

using System.Xml;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Utilities;

using Microsoft.SharePoint.WebControls;

using System.ComponentModel;

using System.Web.UI.WebControls.WebParts;

using System.IO;

using System.Web.UI;

namespace LoadTest

{

[DefaultProperty("DWPText"),

ToolboxData("<{0}Stick out tongueartLoad runat=server></{0}Stick out tongueartLoad>"),

XmlRoot(Namespace = "LoadTest")]

public class PartLoad : Microsoft.SharePoint.WebPartPages.WebPart

{

private string _mDWPText = "";

[Browsable(true), Category("First Chart"),

DefaultValue(""),

WebPartStorage(Storage.Personal),

FriendlyName("Dwp Info"), Description("Webpart Info")]

public string DWPText

{

get

{

return _mDWPText;

}

set

{

_mDWPText = value;

}

}

string ErrorMsg = "";

Microsoft.SharePoint.WebPartPages.WebPart w = null;

protected override void CreateChildControls()

{

base.CreateChildControls();

string HandleMsg = "";

try

{

SPLimitedWebPartManager wpManage =

SPContext.Current.Web.GetLimitedWebPartManager("Pages/Default.aspx",

PersonalizationScope.Shared);

StringReader srData = new StringReader(DWPText);

XmlTextReader xmRead = new XmlTextReader(srData);

w = (Microsoft.SharePoint.WebPartPages.WebPart) wpManage.ImportWebPart(xmRead, out HandleMsg);

this.Controls.Add(w);

}

catch (Exception ex)

{

ErrorMsg = ex.Message + "," + HandleMsg;

}

}

protected override void Render(System.Web.UI.HtmlTextWriter writer)

{

base.Render(writer);

w.RenderControl(writer);

writer.Write("Suc:" + ErrorMsg);

}

}

}