public XsltListViewWebPart AddListToPage(SPList list, string title, string zone, SPLimitedWebPartManager webPartManager, int index)
        {
            // validation
            list.RequireNotNull("list");
            title.RequireNotNullOrEmpty("title");
            zone.RequireNotNullOrEmpty("zone");
            webPartManager.RequireNotNull("webPartManager");
            index.Require(index >= 0, "index");

            XsltListViewWebPart wp = new XsltListViewWebPart();
            wp.ListName = list.ID.ToString("B").ToUpper();
            wp.Title = title;
            wp.ZoneID = zone;
            ModifyViewClass viewOperations = new ModifyViewClass();
            SPView defaultView = viewOperations.GetDefaultView(list);
            SPView modifiedView = viewOperations.CopyView(defaultView, list);
            viewOperations.SetToolbarType(modifiedView, "Standard");
            modifiedView.Update();
            wp.ViewGuid = modifiedView.ID.ToString("B").ToUpper();
            webPartManager.AddWebPart(wp, zone, index);
            list.Update();
            webPartManager.SaveChanges(wp);
            return wp;
        }
        public static void CreateContentEditorWebPart(SPLimitedWebPartManager webPartManager, string Content, string zone, int zoneIndex, PartChromeType chromeType, string webPartTitle)
        {
            // validation
            webPartManager.RequireNotNull("webPartManager");
            Content.RequireNotNullOrEmpty("Content");
            zone.RequireNotNullOrEmpty("zone");
            webPartTitle.RequireNotNullOrEmpty("webPartTitle");

            Guid storageKey = Guid.NewGuid();
            string wpId = String.Format("g_{0}", storageKey.ToString().Replace('-', '_'));
            XmlDocument doc = new XmlDocument();
            XmlElement div = doc.CreateElement("div");
            div.InnerText = Content;
            ContentEditorWebPart cewp = new ContentEditorWebPart { Content = div, ID = wpId, Title = webPartTitle };
            cewp.ChromeType = chromeType;
            webPartManager.AddWebPart(cewp, zone, zoneIndex);
            webPartManager.SaveChanges(cewp);
        }
        public static void ConnectListViewWebParts(SPLimitedWebPartManager webPartManager, ListViewWebPart providerWebPart, ListViewWebPart consumerWebPart, SPRowToParametersTransformer transformer, string consumerInternalFieldName, string providerInternalFieldName)
        {
            webPartManager.RequireNotNull("webPartManager");
            providerWebPart.RequireNotNull("providerWebPart");
            consumerWebPart.RequireNotNull("consumerWebPart");
            transformer.RequireNotNull("transformer");
            consumerInternalFieldName.RequireNotNullOrEmpty("consumerInternalFieldName");
            providerInternalFieldName.RequireNotNullOrEmpty("providerInternalFieldName");

            ProviderConnectionPoint providerConnectionPoint = (from ProviderConnectionPoint conn in webPartManager.GetProviderConnectionPoints(providerWebPart)
                                                               where String.Equals("Provide Row To", conn.DisplayName, StringComparison.OrdinalIgnoreCase) && conn.InterfaceType == typeof(IWebPartRow)
                                                               select conn).FirstOrDefault();
            ConsumerConnectionPoint consumerConnectionPoint = (from ConsumerConnectionPoint conn in webPartManager.GetConsumerConnectionPoints(consumerWebPart)
                                                               where String.Equals("Get Sort/Filter From", conn.DisplayName, StringComparison.OrdinalIgnoreCase) && conn.InterfaceType == typeof(IWebPartParameters)
                                                               select conn).FirstOrDefault();

            consumerWebPart.Connections = consumerWebPart.ConnectionID + "," + providerWebPart.ConnectionID + "," +
                                             consumerConnectionPoint.ID + "," + providerConnectionPoint.ID + "," +
                                             consumerConnectionPoint.ID + "," + providerConnectionPoint.ID + "," +
                                              consumerInternalFieldName + "=" + providerInternalFieldName;

            webPartManager.SaveChanges(consumerWebPart);
        }
        public static void AddWebPart(SPLimitedWebPartManager webPartManager, System.Web.UI.WebControls.WebParts.WebPart webPart, string zone, int zoneIndex, PartChromeType chromeType, string accesskey)
        {
            webPartManager.RequireNotNull("webPartManager");
            webPart.RequireNotNull("webPart");
            zone.RequireNotNullOrEmpty("zone");
            zoneIndex.Require(zoneIndex >= 0, "zoneIndex");

            webPart.AccessKey = accesskey;
            webPart.ChromeType = chromeType;
            webPartManager.AddWebPart(webPart, zone, zoneIndex);
            webPartManager.SaveChanges(webPart);
        }
        public static void AddListToPage(SPList list, string title, string zone, SPLimitedWebPartManager webPartManager, int index, string viewName)
        {
            // validation
            list.RequireNotNull("list");
            title.RequireNotNullOrEmpty("title");
            webPartManager.RequireNotNull("webPartManager");
            viewName.RequireNotNullOrEmpty("viewName");

            ListViewWebPart wp = new ListViewWebPart();
            wp.ListName = list.ID.ToString("B").ToUpper();
            wp.ViewGuid = list.Views[viewName].ID.ToString("B").ToUpper();
            wp.Title = title;
            wp.ZoneID = zone;
            webPartManager.AddWebPart(wp, zone, index);
            list.Update();
            webPartManager.SaveChanges(wp);
        }