Add web part into web part page dynamically

Add web part into web part page dynamically

Post by liuy » Thu, 04 Nov 2004 08:36:23


i,
I have a set of web parts (runat server) and I want only one web part
to sit on my web part page at one time according to user's selection.
I have made a master web part whose UI is like this: a dropdown list
which contains items such as Web part 1, Web part 2, .... When the
selected index changes, the previous web part will be deleted from the
page and the selected web part will be added into the page.

I have tried to use the WebPartPagesWebService to add the selected web
part into my page. Below is a code snippet I have for this:

====================================================================
Sub AddSelectedWebParts(ByVal selectedWebPartXMLFileName As String)
'get the current
Dim curWeb As SPWeb = SPControl.GetContextWeb(context)
Dim curPageUrl As String = curWeb.Url
Dim webpartXML As String =
LoadFile(selectedWebPartXMLFileName)

Dim svc As New mySharepointServer.WebPartPagesWebService

Dim credentail As New
NetworkCredential(Me._sImpersonateLoginName, Me._sImpersonateLoginPWD)

svc.Credentials = New
NetworkCredential(_sImpersonateLoginName, _sImpersonateLoginPWD, "AD")

Dim newPartID As Guid = svc.AddWebPart(curPageUrl, webpartXML,
mySharepointServer.Storage.Shared)
End Sub

here, the body of selectedWebPartXMLFileName is the file I export from
the corresponding web part.

But each time svc.AddWebPart is executed, I get exception. The message
is:

==========================================================================
Exception of type Microsoft.SharePoint.SoapServer.SoapServerException
was thrown.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.Web.Services.Protocols.SoapException:
Exception of type Microsoft.SharePoint.SoapServer.SoapServerException
was thrown.

Source Error:

An unhandled exception was generated during the execution of the
current web request. Information regarding the origin and location of
the exception can be identified using the exception stack trace below.

Stack Trace:


[SoapException: Exception of type
Microsoft.SharePoint.SoapServer.SoapServerException was thrown.]
System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage
message, WebResponse response, Stream responseStream, Boolean
asyncCall)
System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String
methodName, Object[] parameters)
FacOpsWebPartsSelector.facops14.WebPartPagesWebService.AddWebPart(String
pageUrl, String webPartXml, Storage storage) in
C:\WebPartsProjects\FacOpsWebPartsSelector\Web
References\facops14\Reference.vb:96
FacOpsWebPartsSelector.FacOpsWebPartsSelector.AddSelectedWebParts(String
selectedWebPartXMLFileName) in
C:\WebPartsProjects\FacOpsWebPartsSelector\FacOpsWebPartsSelector.vb:117
FacOpsWebPartsSelector.FacOpsWebPartsSelector.drplstFacOpsWebPartsSelector_SelectedIndexChanged(Object
sender, EventArgs e) in
C:\WebPartsProjects\FacOpsWebPartsSelector\FacOpsWebPartsSelector.vb:133
System.EventHandler.Invoke(Object sender, EventArgs e) +0
System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(EventArgs
e) +108
System.Web.UI.WebControls.DropDownList.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent()
+26
System.Web.UI.Pag
 
 
 

Add web part into web part page dynamically

Post by RGFuaWVsIE » Thu, 04 Nov 2004 09:28:01

uping,

You can also use SPFile to get a reference to the webpartpage and use the
SPFile's SPWebPartCollection to add or edit webparts on the page (which is an
SPFile object).

Daniel Larson

"Yuping Liu" wrote:


 
 
 

Add web part into web part page dynamically

Post by RGFuaWVsIE » Thu, 04 Nov 2004 09:28:02

uping,

You can also use SPFile to get a reference to the webpartpage and use the
SPFile's SPWebPartCollection to add or edit webparts on the page (which is an
SPFile object).

Daniel Larson

"Yuping Liu" wrote:

 
 
 

Add web part into web part page dynamically

Post by RGFuaWVsIE » Thu, 04 Nov 2004 09:28:03

uping,

You can also use SPFile to get a reference to the webpartpage and use the
SPFile's SPWebPartCollection to add or edit webparts on the page (which is an
SPFile object).

Daniel Larson

"Yuping Liu" wrote:

 
 
 

Add web part into web part page dynamically

Post by liuy » Fri, 05 Nov 2004 08:22:45

i, Daniel,
Thanks for your help.
I have tried to use SPFile to add web part into the page using the
code:

Dim newWebPartID As Guid = myPageWebParts.Add(dwpFilePath &
dwpFileName)

Here the dwpFile is the file I used to import the web part into page
manually and it work fine in that case. However, I get error message
when above code executed. The error message is below:

======================================================================
The file you imported is not valid. Verify that the file is a Web Part
description file (.dwp) and that it contains well-formed XML.

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

I am sure the file I imported is valid because it works when I import
it into page manually. I have given everyone the read/write
persimmision to the directory where the dwp file resides (it is the
map path of the /wpresources/FacOpsWebPartsSelector/ virtual directory
in my case).
Any idea on what I am missing or wrong, please?

Thanks again for your idea and suggestion.

Yuping

"Daniel Larson (www.portalbuilder.org)" < XXXX@XXXXX.COM > wrote in message news:< XXXX@XXXXX.COM >...
 
 
 

Add web part into web part page dynamically

Post by RGFuaWVsIE » Sat, 13 Nov 2004 08:12:01

Yuping,

Here is a code example, where you can replace ExampleWebPart with a
reference to your own WebPart class. Notice that I'm creating a WebPart
reference and not using a DWP.

(This is my code from a Developer Care event from Born, www.born.com. Thanks
to Born for the code.)

SPSite site = new SPSite("http://localhost");
using (site)
{
SPWeb web = site.OpenWeb("/sites/dev");
using (web)
{
SPFile file = web.GetFile("default.aspx");
SPWebPartCollection parts =
file.GetWebPartCollection(Storage.Shared);

#region add parts programatticly

Born.DevCare.WebParts.Examples.ExampleWebPart newPart =
new Born.DevCare.WebParts.Examples.ExampleWebPart();
newPart.ZoneID = "Left";
Guid storageKey = parts.Add(newPart);
parts.SaveChanges(storageKey);

#endregion

foreach(WebPart part in parts)
{
Console.WriteLine(part.GetType().Name);
Console.WriteLine(" Zone: " + part.ZoneID);
}
}
}