Embed Youtube Video in PageLayout in Sharepoint 2010

In Sharepoint you can Play YouTube Video by using Content Editor Webpart.There are nice article here and here.
My goal was to make easy for end users so i decided to go with event handler which will set Content Property of Content Editor Webpart once users checked in Page.I have PageLayout based on Video Content Type and this content type i have created Site Column called EmbedCode of type Multiple line of text.

 public override void ItemCheckingIn(SPItemEventProperties properties) {
          // base.ItemCheckingIn(properties);
         //  SPWeb web = properties.OpenWeb();
           SPFile file = properties.ListItem.File;
           using (SPLimitedWebPartManager mgr = file.GetLimitedWebPartManager(PersonalizationScope.Shared))  {
                   if (mgr != null) {
                        foreach (Microsoft.SharePoint.WebPartPages.WebPart webpart in mgr.WebParts) {
                           if (webpart.GetType().ToString().Equals("Microsoft.SharePoint.WebPartPages.ContentEditorWebPart")) {
                                ContentEditorWebPart contentEditor = (ContentEditorWebPart)webpart;
                               switch (contentEditor.Title) {
                                   case "Video1":
                                        XmlElement embedCode1 = new XmlDocument().CreateElement("embedCode1");
                                        embedCode1.InnerText = properties.ListItem["EmbedCode1"].ToString();
                                        contentEditor.Content = embedCode1;
                                        mgr.SaveChanges(contentEditor);
                                       break;
                                   case "Video2":
                                       XmlElement embedCode2 = new XmlDocument().CreateElement("embedCode2");
                                       embedCode2.InnerText = properties.ListItem["EmbedCode2"].ToString();
                                       contentEditor.Content = embedCode2;
                                       mgr.SaveChanges(contentEditor);
                                       break;
                                   default:
                                       break;
                               }

                           }
                       }

                   }
               }
           }

Now let’s register Event Handler with Content type.

 public override void FeatureActivated(SPFeatureReceiverProperties properties) {

            SPSite site = properties.Feature.Parent as SPSite;
            using (SPWeb web = site.OpenWeb()) {
                SPContentType VideoCT = web.ContentTypes["Video Publicity Page"];
                VideoCT.EventReceivers.Add(SPEventReceiverType.ItemCheckingIn, "CAS.UpdateCEWP.EventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1dce36753eab5b25", "CAS.UpdateCEWP.EventHandler.PageEventHandler");
                VideoCT.Update(true);
               // web.Update();
            }

        }

        // Uncomment the method below to handle the event raised before a feature is deactivated.

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties) {
            SPSite site = properties.Feature.Parent as SPSite;
            string assemblyName = "CAS.UpdateCEWP.EventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1dce36753eab5b25";
            using (SPWeb web = site.OpenWeb()) {
                SPContentType VideoCT = web.ContentTypes["Video Publicity Page"];
                for (int i = 0; i < VideoCT.EventReceivers.Count; i++) {
                    if (VideoCT.EventReceivers[i].Assembly.Equals(assemblyName)) {
                        VideoCT.EventReceivers[i].Delete();
                    }
                }
                VideoCT.Update(true);
                //web.Update();
            }

        }

Comments and suggestion are welcome.
hope this help.
Thanks
Ronak

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s