Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

What I'm trying to do:

I've got a custom aspx page that is wrapped in a SharePoint feature in order to get deployed. This aspx page contains 4 (Excel Web Access) web parts that are added dynamically (I could have added them through SharePoint Designer, that's true, but I wanted more control). As you will see below, there are weekly updated web parts and monthly updated web parts. What I would like to achieve in the end, depending on the HttpContext.Current.User, is either show only the weekly or the monthly web parts. The remaining two should be kept hidden (through webPart.hidden = true). Since the Excel Web Access web part will throw an error if no required "named item" was found in the underlying Excel sheet, I will only know whether to show a particular web part after it has fetched its data.

What I've tried so far:

  • Events

In order to avoid the user being shown 4 web parts with two of them disappearing again after everything has been loaded, I've tried to use web part events or web part zone events in order to be able to pinpoint the moment where all of the web parts have been loaded but still not being shown. My events are not caught by the debugger, though. The code I have used is shown below. I've based myself on the following Event Handling Schema.

  • Excel Web Services

Another option that I was pondering upon was to use Excel Web Services to check whether the named items are present in a particular Excel sheet. I have no experience trying this approach, so I wondered whether anyone out there has.

namespace Reynaers.SharePoint.ASP.NET.CustomPages
{
    public class CustomEWAPage : LayoutsPageBase
    {
        #region Constructors

    public CustomEWAPage()
    {
        AddWebParts();
    }

    #endregion Constructors

    #region Methods


    private void ApplySecurity()
    {
        //todo
    }

    private void AddWebParts()
    {
        AddWebPartIfNotExists("wpZone", 1, "Delivery Performance Graph Weekly");
        AddWebPartIfNotExists("wpZone", 2, "Delivery Performance Graph Monthly");
        AddWebPartIfNotExists("wpZone", 3, "Delivery Performance Data Weekly");
        AddWebPartIfNotExists("wpZone", 4, "Delivery Performance Data Monthly");
    }

    private void AddWebPartIfNotExists(string zoneId, int zoneIndex, string webPartTitle)
    {
        SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                if (!WebPartExists(webPartTitle))
                {
                    string currentUrl = HttpContext.Current.Request.Url.ToString();
                    using (SPSite site = new SPSite(currentUrl))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            web.AllowUnsafeUpdates = true;
                            //create new webpart object           
                            ExcelWebRenderer wp = new ExcelWebRenderer();

                            //set properties of new webpart object     
                            //misuse of tooltip property... (title did not work for me, but that's another story)
                            wp.ToolTip = webPartTitle;
                            wp.ChromeState =System.Web.UI.WebControls.WebParts.PartChromeState.Normal;
                            wp.ChromeType =System.Web.UI.WebControls.WebParts.PartChromeType.None;

                            //add new webpart object to webparts collection     
                            SPLimitedWebPartManager mgr =  web.GetLimitedWebPartManager(currentUrl, PersonalizationScope.Shared);
                            mgr.AddWebPart(wp, zoneId, zoneIndex);
                            web.Update();
                            mgr.SaveChanges(wp);

                            //attach events
                            wp.PreRender += new System.EventHandler(wp_PreRender);
                            wp.Load += new System.EventHandler(wp_Load);
                            mgr.SaveChanges(wp);
                            wp.Zone.PreRender += new System.EventHandler(Zone_PreRender);

                            //update spWeb object     
                            web.AllowUnsafeUpdates = false;
                            web.Update();
                        }
                    }
                }
            }
        );
    }

    void Zone_PreRender(object sender, System.EventArgs e)
    {
        //just to check whether we arrive at this point...
        throw new System.NotImplementedException();
    }

    void wp_Load(object sender, System.EventArgs e)
    {
        //just to check whether we arrive at this point...
        ExcelWebRenderer webPart = sender as ExcelWebRenderer;            
    }

    void wp_PreRender(object sender, System.EventArgs e)
    {
        //just to check whether we arrive at this point...
        ExcelWebRenderer webPart= sender as ExcelWebRenderer;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <info>Kindly based upon http://www.etechplanet.com/blog/addingremoving-web-part-programmatically-in-sharepoint-website-using-c-code.aspx</info>
    /// <param name="webPartTitle">Title of the web part that needs to be checked for existence</param>
    private bool WebPartExists(string webPartTitle)
    {
        string currentUrl = HttpContext.Current.Request.Url.ToString();
        using (SPSite site = new SPSite(currentUrl))
        {
            using (SPWeb web = site.OpenWeb())
            {
                SPLimitedWebPartCollection webparts = web.GetLimitedWebPartManager(currentUrl, PersonalizationScope.Shared).WebParts;
                for (int k = 0; k < webparts.Count; k++)
                {
                    //get reference to webpart
                    System.Web.UI.WebControls.WebParts.WebPart wp = webparts[k];
                    //check webpart Title to find webpart which is to be removed
                    if (wp.ToolTip.ToUpper().Equals(webPartTitle.ToUpper()))
                    {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    #endregion Methods
}

}

share|improve this question

migrated from stackoverflow.com Jun 27 '12 at 13:29

This question came from our site for professional and enthusiast programmers.

add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.