Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Inserting CEWP on MySite Creation

$
0
0

Hello all,

I have a requirement that I need to insert a CEWP in MySites upon their creation. I am currently using feature stapling to acheive this goal, and I have been successful in getting the feature deployed, and activated to the MySite on it's creation, but my CEWP is not being created on the personal site (Targeting the personal sites here, not the Profile sites)

I have created a feature with an event receiver. The receiver has the following code: (Courtesy of http://www.greggalipeau.com/2011/07/12/sharepoint-provision-default-webparts-for-mysites/comment-page-1/#comment-1616)

using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using System.Threading;
using Microsoft.SharePoint.WebPartPages;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;



namespace StaplerNewest.Features.CEWPSSO
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("5e94a539-af4b-44bc-99a2-2e0c3e324432")]
    public class CEWPSSOEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(RunProcess), properties);
        }
        private void RunProcess(object state)
        {
            SPFeatureReceiverProperties properties = (SPFeatureReceiverProperties)state;
                using (SPSite site = (SPSite)properties.Feature.Parent)
                {
                    bool provisioned = false;
                    while (provisioned == false)
                    {
                        provisioned = site.RootWeb.Provisioned;
                        Thread.Sleep(5000);
                    };

                    AddWebPart(site.RootWeb.Url);
                }
        }

        private void AddWebPart(string url)
        {
            using (SPSite site = new SPSite(url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    try
                    {
                        SPLimitedWebPartManager wpm = web.GetLimitedWebPartManager("default.aspx", PersonalizationScope.Shared);

                        bool webpartexists = false;
                        foreach (Microsoft.SharePoint.WebPartPages.WebPart webpart in wpm.WebParts)
                        {
                            if (webpart.Title == "ifoliosso")
                            {
                                webpartexists = true;
                                break;
                            }
                        }

                        if (!webpartexists)
                        {
                            ContentEditorWebPart ifoliosso = new ContentEditorWebPart();
                            ifoliosso.ChromeType = PartChromeType.None;
                            ifoliosso.Title = "ifoliosso";
                            wpm.AddWebPart(ifoliosso, "TopZone", 0);
                            wpm.AddWebPart(ifoliosso, "MiddleRightZone", 0);
                            wpm.AddWebPart(ifoliosso, "MiddleLeftZone", 0);
                            wpm.SaveChanges(ifoliosso);

                        }
                    }
                    catch
                    {
                        //do nothing
                    }
                }
            }
        }
    }
}


Like I said before, the Feature itself is being activated on the MySite, but the CEWP is not created. Also, here is my stapler code:

<?xml version="1.0" encoding="utf-8"?><Elements xmlns="http://schemas.microsoft.com/sharepoint/"><FeatureSiteTemplateAssociation Id="f6924d36-2fa8-4f0b-b16d-06b7250180fa" TemplateName="SPSPERS#2"/><FeatureSiteTemplateAssociation Id="db673cd5-c39a-4345-b989-0bd5cce704f3" TemplateName="SPSPERS#2"/></Elements>

Since the feature is being activated (ID db673cd5-c39a-4345-b989-0bd5cce704f3) i'm assuming it's the code in my event receiver, but I'm not sure. Any help would be greatly appreciated!

-Nick


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>