0

Once I have an instance of an object "System.Web.UI.WebControls.WebParts.WebPart", How can I add it into a _layout page created in VisualStudio. I have tried with

ContentPlaceHolder ContentPlaceHolder = (ContentPlaceHolder)this.Master.FindControl("PlaceHolderMain");
ContentPlaceHolder.Controls.Add(myWebPart);

but it does not work.

UPDATE

I have different users in my site. On my application page (/_layout/ page) y get some information from their ActiveDirectory and I use that information to show different contents. Then I have a SQL table that I display using SharePoint Designer datasources. After that I create an application page on which I insert the datasource as a webpart (on the code it is inserted as ). On each row of that webpart I add an extra column with an hypelink that depends on that row ({@Id}). So I need to use that webpart created on SharePointDesigner on my application page, and then filter dinamically based on the user information. Otherwise I will need to create a sitepage for each of the possible filters (but I don't find a good solution to don't reuse the webpart).

Then I thought, let's go in the code and look for that webpart that I have created. once I find it, get it and show it on my application page. I use the next code to find the webpart:

System.Web.UI.WebControls.WebParts.WebPart personalwp = null;
List<string> lstpages = new List<string>();

SPSite site2 = SPContext.Current.Site;
SPWeb web2 = site2.OpenWeb();
SPList listpages = web2.Lists["Site Pages"];

SPListItemCollection pages = listpages.Items;

foreach (SPListItem page in pages)
{
    lstpages.Add("\n" + page.DisplayName + " \n");
    SPFile file = page.File;
    using (SPLimitedWebPartManager mgr = file.GetLimitedWebPartManager(PersonalizationScope.Shared))
    {
        try
        {
            SPLimitedWebPartCollection webparts = mgr.WebParts;
            foreach (System.Web.UI.WebControls.WebParts.WebPart wp in webparts)
            {
                lstpages.Add(" - " + wp.Title + " ; ");
                if (wp.Title == "Pending Evaluations")
                {
                    personalwp = wp;
                }
            }
        }
        finally
        {
            // Needs to be disposed
            //mgr.Web.Dispose();
        }

    }
}

Once I have the webpart on my object 'personalwp'I try to add it with:

ContentPlaceHolder ContentPlaceHolder = (ContentPlaceHolder)this.Master.FindControl("PlaceHolderMain");
ContentPlaceHolder.Controls.Add(myWebPart);

@Alexander: I don't think I can use the code taht you propose in the your first link on a dynamic way right?

To add the web part just add it directly in MarkUp by registering the tag:

<%@ Register TagPrefix="ABC" Namespace="Namespace" Assembly="Assembly" %>
and directly adding the web part,

<ABC:ClassName ID="ControlID" FrameType="None" runat="server" __WebPartId="YouWebPartGUID" WebPart="true" />

Other possibility would be to add DataFormWebPart Directly on the code and set the filter programatically, but I couldn't find the way neither.

I already use on my site ListViewByQuery for other informations but with the same logic (filter values depending the user) and it works just so easily.


Any other ideas? Thanks

2 Answers 2

1

Provided myWebPart works and you page has a masterpage with a ContentPlaceHolder with the ID PlaceHolderMain the above should work (see my code+picture below).

But why go through the the masterpage?

Normally you'll add a Panel in the markup and then add the controls to that like this:

MyPage.aspx:

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MyPage.aspx.cs" Inherits="WebPartOnLayoutsPage.Layouts.WebPartOnLayoutsPage.MyPage" DynamicMasterPageFile="~masterurl/default.master" %>

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
Test before panel
<asp:Panel ID="Content" runat="server"></asp:Panel>
Test after panel
</asp:Content>

MyPage.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{
    // Adding the hard way
    var cph = this.Master.FindControl("PlaceHolderMain") as ContentPlaceHolder;
    var iwp1 = new ImageWebPart();
    iwp1.Title = "ImageWebPart added to ContentPlaceHolder";
    iwp1.ImageLink = "http://www.develop.com/images/logo-main.jpg";
    cph.Controls.Add(iwp1);

    // Adding the easy way
    var iwp2 = new ImageWebPart();
    iwp2.Title = "ImageWebPart added to Panel";
    iwp2.ImageLink = "http://www.develop.com/images/logo-main.jpg";
    Content.Controls.Add(iwp2);
}

This results in: enter image description here

2
  • Thanks for the tip about using asp:Panel, it's much logic. The problem is when I try to add a "ListViewByQuery" it does not work with the Panel but it does with the ContentPlaceHolder.
    – pepersview
    Mar 19, 2012 at 9:44
  • About the Webpart, it still does not work. I will update my post with the code that I'm using.
    – pepersview
    Mar 19, 2012 at 9:45
0

You can add this web part like describes in this post. But I think that if you add a web part to an application page, you are doing something wrong ))) You can read difference between application and web part pages here.

2
  • Thanks for the link. After reading the second one I think that I was understanding good the difference between all of them, but in my case I think I should use it on that way. I will explain more detailed in the Update of my question.
    – pepersview
    Mar 19, 2012 at 9:46
  • About the first link, I can not use that because I need to add the webpart dynamically, depending on the user.
    – pepersview
    Mar 19, 2012 at 9:47

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .