Adding web part programmatically : Sharepoint
This is a discussion on Adding web part programmatically within the Sharepoint forums in Microsoft Tools category; Dear All, i'm using the following code to add web parts to a page programmatically, however i've a problem that after the page is postedback the web part is not visible on the page, i need to refresh the page to get the web part visible, appreciate your support. SPWeb myWebSite = SPContext.Current.Web; SPLimitedWebPartManager WebPartMgr = myWebSite.GetLimitedWebPartManager(Page.Request.Ra wUrl, PersonalizationScope.Shared); SPSite s = new SPSite(myWebSite.Url); SPList WebPartCatalog = s.GetCatalog(SPListTemplateType.WebPartCatalog); string serverUrl = ""; //tohandle the root url in case of mapped pathes serverUrl = myWebSite.Url.Substring(0, myWebSite.Url.LastIndexOf("/") ); StreamReader sr = new StreamReader(new System.Net.WebClient().OpenRead("http://" + s.HostName +"/" + WebPartCatalog.Items.GetItemById(Convert.ToInt16(d rpWebParts.SelectedValue)).File.Url)); StringReader strR ...
| Sharepoint Microsoft sharepoint portal server development, administration and related discussions |
![]() |
| | LinkBack | Thread Tools |
|
#1
| |||
| |||
| i'm using the following code to add web parts to a page programmatically, however i've a problem that after the page is postedback the web part is not visible on the page, i need to refresh the page to get the web part visible, appreciate your support. SPWeb myWebSite = SPContext.Current.Web; SPLimitedWebPartManager WebPartMgr = myWebSite.GetLimitedWebPartManager(Page.Request.Ra wUrl, PersonalizationScope.Shared); SPSite s = new SPSite(myWebSite.Url); SPList WebPartCatalog = s.GetCatalog(SPListTemplateType.WebPartCatalog); string serverUrl = ""; //tohandle the root url in case of mapped pathes serverUrl = myWebSite.Url.Substring(0, myWebSite.Url.LastIndexOf("/") ); StreamReader sr = new StreamReader(new System.Net.WebClient().OpenRead("http://" + s.HostName +"/" + WebPartCatalog.Items.GetItemById(Convert.ToInt16(d rpWebParts.SelectedValue)).File.Url)); StringReader strR = new StringReader(sr.ReadToEnd()); XmlReader xmlR = XmlReader.Create(strR); string errMsg = ""; System.Web.UI.WebControls.WebParts.WebPart wp = WebPartMgr.ImportWebPart(xmlR, out errMsg); WebPartMgr.AddWebPart(wp, drpZones.SelectedValue , 0); |
|
#2
| |||
| |||
| Have you tried redirecting the page back to itself after your code runs? -- Bryan Phillips MCT, MCSD, MCDBA, MCSE Microsoft MVP - Client Application Development Blog: http://bphillips76.spaces.live.com Web Site: http://www.composablesystems.net "Mohamed Zaki" <m_o_z_a_k_i@mail.link.net> wrote in message news:Oz2TZPbMIHA.280@TK2MSFTNGP03.phx.gbl: > Dear All, > > i'm using the following code to add web parts to a page programmatically, > however i've a problem that after the page is postedback the web part is > not visible on the page, i need to refresh the page to get the web part > visible, > > appreciate your support. > > SPWeb myWebSite = SPContext.Current.Web; > > > > SPLimitedWebPartManager WebPartMgr = > myWebSite.GetLimitedWebPartManager(Page.Request.Ra wUrl, > PersonalizationScope.Shared); > > SPSite s = new SPSite(myWebSite.Url); > > SPList WebPartCatalog = s.GetCatalog(SPListTemplateType.WebPartCatalog); > > string serverUrl = ""; //tohandle the root url in case of mapped pathes > > serverUrl = myWebSite.Url.Substring(0, myWebSite.Url.LastIndexOf("/") ); > > > > StreamReader sr = new StreamReader(new > System.Net.WebClient().OpenRead("http://" + s.HostName +"/" + > WebPartCatalog.Items.GetItemById(Convert.ToInt16(d rpWebParts.SelectedValue)).File.Url)); > > StringReader strR = new StringReader(sr.ReadToEnd()); > > > > XmlReader xmlR = XmlReader.Create(strR); > > string errMsg = ""; > > System.Web.UI.WebControls.WebParts.WebPart wp = > WebPartMgr.ImportWebPart(xmlR, out errMsg); > > WebPartMgr.AddWebPart(wp, drpZones.SelectedValue , 0); |
|
#3
| |||
| |||
| Yes i did, but then i get the page in the browse mode not in the edit mode, actually i posted another question in this group asking about how to redirect to the page in the edit mode. Thank you for your reply Bryan regards, Mohamed "Bryan Phillips" <bphillips@nospam.spamcop.net.spammenot> wrote in message news:%23p4tcXhMIHA.6108@TK2MSFTNGP03.phx.gbl... > Have you tried redirecting the page back to itself after your code runs? > > -- > Bryan Phillips > MCT, MCSD, MCDBA, MCSE > Microsoft MVP - Client Application Development > Blog: http://bphillips76.spaces.live.com > Web Site: http://www.composablesystems.net > > > > "Mohamed Zaki" <m_o_z_a_k_i@mail.link.net> wrote in message > news:Oz2TZPbMIHA.280@TK2MSFTNGP03.phx.gbl: > >> Dear All, >> >> i'm using the following code to add web parts to a page programmatically, >> however i've a problem that after the page is postedback the web part is >> not visible on the page, i need to refresh the page to get the web part >> visible, >> >> appreciate your support. >> >> SPWeb myWebSite = SPContext.Current.Web; >> >> >> >> SPLimitedWebPartManager WebPartMgr = >> myWebSite.GetLimitedWebPartManager(Page.Request.Ra wUrl, >> PersonalizationScope.Shared); >> >> SPSite s = new SPSite(myWebSite.Url); >> >> SPList WebPartCatalog = s.GetCatalog(SPListTemplateType.WebPartCatalog); >> >> string serverUrl = ""; //tohandle the root url in case of mapped pathes >> >> serverUrl = myWebSite.Url.Substring(0, myWebSite.Url.LastIndexOf("/") ); >> >> >> >> StreamReader sr = new StreamReader(new >> System.Net.WebClient().OpenRead("http://" + s.HostName +"/" + >> WebPartCatalog.Items.GetItemById(Convert.ToInt16(d rpWebParts.SelectedValue)).File.Url)); >> >> StringReader strR = new StringReader(sr.ReadToEnd()); >> >> >> >> XmlReader xmlR = XmlReader.Create(strR); >> >> string errMsg = ""; >> >> System.Web.UI.WebControls.WebParts.WebPart wp = >> WebPartMgr.ImportWebPart(xmlR, out errMsg); >> >> WebPartMgr.AddWebPart(wp, drpZones.SelectedValue , 0); > |
|
#4
| |||
| |||
| You can redirect to the same page after u finish all ur functionality. You need not to worry about shared mode or Edit mode. Since you are redirecting to the same page, u have to specify some condition so that page should not go infinite loop in redirction. You can simply use Query string for the same. For Example: if (Request.QueryString["success"] == null) { Response.Redirect("/YourPage.aspx?success=1"); } Regards, Hema |
|
#5
| |||
| |||
| SharePoint site security helps manage permissions for different resources within a site by defining the levels of accessibility permissions for different peoples and groups. In SharePoint, always the top-level or the parent level permissions are inherited to it's child contents (e.g. a sub-site inheriting permissions from it's parent site collection).In-order to create unique permission we need to break the inheriting parent permission and create new permission level for the SharePoint content. These permissions can be defined for specific users or groups. Please add on . Thanks Eliza |
![]() |
| Thread Tools | |
| |
| ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Issue With Programmatically Impersonating a User in a Web-Part | usenet | Sharepoint | 6 | 02-17-2010 04:51 AM |
| Adding data to a subform programmatically | usenet | ADO DAO RDO RDS | 6 | 10-22-2007 02:44 AM |
| Trouble adding ListView web part in a custom web part | usenet | Sharepoint | 0 | 10-18-2007 06:47 AM |
| Programmatically Changing the URL of a Page Viewer web part | usenet | Sharepoint | 1 | 01-24-2007 05:25 PM |
| Programmatically specify client cert as part of web service call | usenet | Java | 2 | 01-11-2004 08:31 AM |




