July 21, 2012

Creation of webpart page and content editor webpart programatically in sharepoint 2010

 DynamicDL
 public static bool insert(PropellerStudioBL propellerStudioBL)
        {

            bool insert = false;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (MysiteDataContext context = new MysiteDataContext(SiteHelper.SiteUrl))
                {

                    PropellerItem propellerStudioItem = new PropellerItem()

                    {

                     Title  = propellerStudioBL.Propellertitle,
                     

                    };
                    context.Propeller.InsertOnSubmit(propellerStudioItem);
                    context.SubmitChanges();

                    if (propellerStudioItem.Id > 0)
                    {
                        insert = true;
                        if (propellerStudioBL.Title != null && propellerStudioBL.Title != string.Empty)
                        {
                           
                        }
                        else
                        {
                            insert = true;
                        }
                    }
                }
            });
            return insert;
        }
     public static System.Data.DataTable GetPropellerData(PropellerStudioBL propellerStudioBL)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Title");
            dt.Columns.Add("Url");
            DataRow row;
            SPSite spsite;

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {


                using (VCSBINTRANETDataContext context = new VCSBINTRANETDataContext(SiteHelper.SiteUrl))
                {

                    var quer = from propellerda in context.PropellerStudio
                               orderby propellerda.Id descending
                               select propellerda;

                    if (quer != null)
                    {
                        foreach (PropellerStudioItem item in quer.Take(3))
                        {
                            row = dt.Rows.Add();
                            row["Title"] = Convert.ToString(item.Title);
                            row["Url"] = Convert.ToString(SPContext.Current.Web.Url + "/pages/" + item.Title + ".aspx");
                        }
                    }
                }
            });
            return dt;
        }

Gridview

           <asp:GridView ID="grdPropeller"  Width="100%" runat="server" BorderStyle="None" 
               AutoGenerateColumns="False" GridLines="None" EmptyDataText="There are no items to show in this view."
               ShowHeader="False">
               <Columns>
           
                   <asp:TemplateField>
                       <ItemStyle Width="350px" />
                       <ItemTemplate>
                           <asp:HyperLink ID="HypTitle" NavigateUrl='<%# Eval("Url") %>' runat="server">
                           <%# Eval("Title")%>                                              
                     
                       <br />
                          </ItemTemplate>
                   </asp:TemplateField>
               </Columns>
           </asp:GridView>
      
 
 Function to add A contenteditorwebpart dynamically

protected void createcontenteditorwebpart(string pagename)
        {
            SPSite site = SPContext.Current.Site;
                using (SPWeb web = site.OpenWeb())
                {
                            

                    SPList list = web.Lists["Pages"];
                    string postInformation =

                      "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +

                      "<Method>" +

                        "<SetList Scope=\"Request\">" + list.ID + "</SetList>" +

                        "<SetVar Name=\"ID\">New</SetVar>" +

                        "<SetVar Name=\"Cmd\">NewWebPage</SetVar>" +

                        "<SetVar Name=\"Type\">WebPartPage</SetVar>" +

                        "<SetVar Name=\"WebPartPageTemplate\">1</SetVar>" +

                        "<SetVar Name=\"Title\">" + pagename + "</SetVar>" +

                        "<SetVar Name=\"Overwrite\">true</SetVar>" +

                      "</Method>";
                    string processBatch = web.ProcessBatchData(postInformation);
                    web.Update();
           
                }
               
          
        }
Function to creat A webpartPage 
 public void Createwebpartpage(string title)
        {
          
            createcontenteditorwebpart(title);
            SPFile page = SPContext.Current.Web.GetFile("/Pages/" + title + ".aspx");
            using (SPLimitedWebPartManager wpmgr = page.GetLimitedWebPartManager(PersonalizationScope.Shared))
            {
                XmlElement p = new XmlDocument().CreateElement("p");
                p.InnerText = "Edit this webpart to add content on this Page";
              

                ContentEditorWebPart cewp = new ContentEditorWebPart
                {
                    Content = p
                };
                wpmgr.AddWebPart(cewp, "Header", 0);
            }
            page.Update();
         

        }
Note:
1.Call  Createwebpartpage(txtPropellerTitle.Text);function in submit button.
2.Add Microsoft.sharepoint.publishing dll in reference

Sample for creating a a Article page programatically
 public void createuserwiki(string title)
        {
            SPWeb spweb = SPContext.Current.Web;

            PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(spweb);
            string pageName = title + ".aspx";

            PageLayout[] pageLayouts = publishingWeb.GetAvailablePageLayouts();
            PageLayout currPageLayout = pageLayouts[9];
            PublishingPageCollection pages = publishingWeb.GetPublishingPages();
          
           PublishingPage newPage = pages.Add(pageName, currPageLayout);

            newPage.ListItem[FieldId.PublishingPageContent] = "";
            newPage.ListItem[FieldId.RollupImage] = "";
            newPage.ListItem[FieldId.Title] = "";

            newPage.ListItem.Update();
            newPage.Update();
            newPage.CheckIn("This is just a comment");
            SPFile page = SPContext.Current.Web.GetFile("/Pages/" + title + ".aspx");
            using (SPLimitedWebPartManager wpmgr = page.GetLimitedWebPartManager(PersonalizationScope.Shared))
            {
                XmlElement p = new XmlDocument().CreateElement("p");
                p.InnerText = "Edit this webpart to add content on this Page";
                // page.DeleteProperty(pageLayouts[0]);

                ContentEditorWebPart cewp = new ContentEditorWebPart
                {
                    Content = p
                };
                wpmgr.AddWebPart(cewp, "Header", 0);





            }

No comments:

Post a Comment