Objectmix
Tags Register Mark Forums Read

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.RawUrl, 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(drpWebParts.SelectedValue)).File.Url)); StringReader strR = new ...


Object Mix > Microsoft Tools > Sharepoint > Adding web part programmatically

Sharepoint Microsoft sharepoint portal server development, administration and related discussions

Reply

 

LinkBack Thread Tools
  #1  
Old 11-28-2007, 06:43 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Adding web part programmatically

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.RawUrl,
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(drpWebParts.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  
Old 11-28-2007, 06:24 PM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: Adding web part programmatically

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.RawUrl,
> 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(drpWebParts.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  
Old 11-28-2007, 06:48 PM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: Adding web part programmatically

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.RawUrl,
>> 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(drpWebParts.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  
Old 08-30-2008, 06:18 AM
Junior Member
 
Join Date: Aug 2008
Posts: 1
hemajyothi is on a distinguished road
Default Re: Adding web part programmatically

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  
Old 02-09-2010, 08:34 AM
Member
 
Join Date: Feb 2010
Posts: 49
elizas is on a distinguished road
Default Re: Adding web part programmatically

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
__________________
Cheers,
Eliza
Reply

Thread Tools


Similar Threads

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


All times are GMT -5. The time now is 08:35 AM.

Managed by Infnx Pvt Ltd.