0

I have a module for Stapling features in SPSPERS#2 (My Sites).

In the Elements.xml of the stapling module I have this:

<!-- My Site contents (Content Types, lists, etc)-->
<FeatureSiteTemplateAssociation Id="fc57dad0-c4d0-4827-9ade-eff87b9e6d3b" TemplateName="SPSPERS#2" />

<!--my site pages (feature with an event receiver that adds XsltListViewWebPart instances 
    to my custom pages-->
<FeatureSiteTemplateAssociation Id="fe02ff99-6b1f-4471-a6cf-a240d3e5fcf5" TemplateName="SPSPERS#2" />

<!--branding y custom masterpage-->
<FeatureSiteTemplateAssociation Id="9a17f4a2-3025-4b14-ae54-d489fe0f2449" TemplateName="SPSPERS#2" />

The problem is that both Custom Lists and Custom Pages are created in the stapling but when I visit one of my custom pages nothing is there. Content only appears if I deactivate the feature (fe02ff99-6b1f-4471-a6cf-a240d3e5fcf5) and then activate throughout the UI.

This is the code of the feature fe02ff99-6b1f-4471-a6cf-a240d3e5fcf5 event receiver:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    if (properties != null)
    {
        SPWeb mySiteWeb = properties.Feature.Parent as SPWeb;
        if (mySiteWeb != null)
        {
            try
            {
                mySiteWeb.AllowUnsafeUpdates = true;

                //Aprovisionamiento de Biblitoeca de Documentos
                ProvisionWebPart(mySiteWeb, "mydocuments.aspx", "Documents", "All", "All Documents");

                //Aprovisionamiento de Calendario
                ProvisionWebPart(mySiteWeb, "myevents.aspx", "Calendar", "Calendar", "Events");

                //Aprovisionamiento de Favoritos
                ProvisionWebPart(mySiteWeb, "myfavourites.aspx", "FavouritesList", "All Items", "My Favourites");
            }
            catch (Exception ex)
            {
            }

            finally
            {
                mySiteWeb.AllowUnsafeUpdates = false;
            }
        }
    }
}

And the code of ProvisionWebPart Method

private static void ProvisionWebPart(SPWeb mySiteWeb, string page, string listName, string viewName, string title)
{
    using (SPLimitedWebPartManager webpartmanager = mySiteWeb.GetLimitedWebPartManager(page, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
    {
        SPList lista = mySiteWeb.Lists[listName];

        XsltListViewWebPart newLvwp = new XsltListViewWebPart();

        newLvwp.ListName = lista.ID.ToString("B").ToUpper();
        newLvwp.ViewGuid = lista.Views[viewName].ID.ToString("B").ToUpper();
        newLvwp.Title = title;

        //listviewwebpart.AllowClose = false;                   
        webpartmanager.AddWebPart(newLvwp, "MiddleZone", 1);

        mySiteWeb.Update();

        newLvwp.Dispose();
    }
}

Any idea why is not showing content on stapling but on deactivating and activating manually?

  • Try logging the exception in feature. Perhaps either lists or pages haven't been provisioned at the time of feature activation. – Nadeem Yousuf Mar 21 '14 at 9:45
  • But it is not supposed to staple in order? when you put the FeatureSiteTemplateAssociation in the elements.xml? – Alvmad Mar 21 '14 at 9:46
  • Yes it should follow the order, but when and where are you provisioning the pages? – Nadeem Yousuf Mar 21 '14 at 9:48
  • The second feature I posted above fe02ff99-6b1f-4471-a6cf-a240d3e5fcf5 has the content in the "Items in the Feature" and also has the event receiver. Would you recommend to add an additional feature for the pages (without the receiver) and the current feature with the already existing receiver? – Alvmad Mar 21 '14 at 9:54
3

Based on your comment, create a separate feature for page provisioning and add it in FeatureSiteTemplateAssociation after the lists are created and put the feature which adds XsltListViewWebPart third in the order. So by the time the code in feature activated event of third feature is called both Lists and Pages will be available.

  • I will test this. Thank you very much. Just one more question. One of the lists is the SkyDrive Library ("Documents") that I want to add to mydocuments.aspx. I think that my features are being activated first and then the SharePoint owned features so the Document Library would not be available. What do you think? – Alvmad Mar 21 '14 at 10:04
  • Not sure about that. – Nadeem Yousuf Mar 21 '14 at 10:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.