protected override void CreateChildControls() { PublishingPageDesignFieldValue itemFieldValue = null;// = (ContentTypeIdFieldValue)this.ItemFieldValue; base.CreateChildControls(); SPWeb web = SPContext.Current.Web; if (base.ControlMode == SPControlMode.Display) { this.lbl_PublishingLayout = (Label)this.TemplateContainer.FindControl("lbl_PublishingLayout"); itemFieldValue = (PublishingPageDesignFieldValue)this.ItemFieldValue; if (itemFieldValue != null) { this.lbl_PublishingLayout.Text = itemFieldValue.Title;//[2]; } } else { this.ddl_PublishingLayout = (DropDownList)this.TemplateContainer.FindControl("ddl_PublishingLayout"); this.lbl_Description = (Label)this.TemplateContainer.FindControl("lbl_Description"); this.PopulateControls("Publishing Content Types");//, layout); try { itemFieldValue = (PublishingPageDesignFieldValue)this.ItemFieldValue; } catch (Exception ex) { ex.ToString(); } if (itemFieldValue != null && !itemFieldValue.IsEmpty) { PageLayout layout = servicePageLayouts.GetPageLayout(new Guid(itemFieldValue[0])); this.ddl_PublishingLayout.SelectedValue = itemFieldValue[0]; } /* * else * { * this.PopulateControls(itemFieldValue.StoredGroup, itemFieldValue.Id); * } */ this.ddl_PublishingLayout.AutoPostBack = true; this.ddl_PublishingLayout.SelectedIndexChanged += new EventHandler(ddl_PublishingLayout_SelectedIndexChanged); } }
protected void Page_Load(object sender, EventArgs e) { if (!base.IsPostBack) { SPWeb web = SPContext.Current.Web; SPWeb rootWeb = SPContext.Current.Site.RootWeb; foreach (SPList list in web.Lists) { if (list.BaseType == SPBaseType.DocumentLibrary) { SPDocumentLibrary docLib = (SPDocumentLibrary)list; if (docLib.IsCatalog == false && !list.IsApplicationList && !list.IsSiteAssetsLibrary && ContainsWebSitePageTypes(list)) { ListItem item = new ListItem(list.Title, list.ID.ToString()); if (!this.ddl_library.Items.Contains(item)) { this.ddl_library.Items.Add(item); } } } } //TODO ListId queryparam try { this.ddl_library.SelectedValue = Request.Params["list"].ToString(); } catch (Exception ex) { ex.ToString(); } SPContentTypeCollection availableContentTypes = rootWeb.AvailableContentTypes; foreach (SPContentType contenttype in availableContentTypes) { if (!this.ddl_contentgroup.Items.Contains(new ListItem(contenttype.Group))) { this.ddl_contentgroup.Items.Add(contenttype.Group); } } this.ddl_contentgroup.Items.Remove("_Hidden"); this.ddl_contentgroup.SelectedValue = "Publishing Content Types"; foreach (SPContentType contenttype in availableContentTypes) { if (contenttype.Group == this.ddl_contentgroup.SelectedValue) { List <PageLayout> check = servicePageLayouts.GetPageLayouts(contenttype.Id); if (check.Count > 0) { ListItem item = new ListItem(contenttype.Name, contenttype.Id.ToString()); this.ddl_contentname.Items.Add(item); } } } if (this.ddl_contentname.Items.Count > 0) { this.ddl_contentname.SelectedIndex = 0;; SPContentTypeId contenttypeid = new SPContentTypeId(this.ddl_contentname.SelectedValue); List <PageLayout> layouts = servicePageLayouts.GetPageLayouts(contenttypeid); //layouts.AddRange(servicePageLayouts.GetPageLayouts(contenttypeid.Parent)); foreach (PageLayout layout in layouts) { ListItem item = new ListItem(layout.Title, layout.UniqueId.ToString()); if (!this.ddl_pagelayout.Items.Contains(item)) { this.ddl_pagelayout.Items.Add(item); } } if (this.ddl_pagelayout.Items.Count > 0) { PageLayout layout = servicePageLayouts.GetPageLayout(new Guid(this.ddl_pagelayout.Items[0].Value)); this.lbl_description.Text = layout.Description; } } } }
public void RaisePostBackEvent(string eventArgument) { SPContext.Current.Web.AllowUnsafeUpdates = true; Guid uniqueId = Guid.Empty; try { string[] eventArg = eventArgument.Split(new string[1] { "#;" }, StringSplitOptions.None); SPWeb rootWeb = SPContext.Current.Site.RootWeb; SPListItem item = SPContext.Current.ListItem; if (!string.IsNullOrEmpty(eventArg[1])) { PageLayout layout = servicePageLayouts.GetPageLayout(new Guid(eventArg[1])); if (item != null && layout != null) { SPFile currentFile = item.File; bool checkout = false; if (currentFile != null && currentFile.Exists) { if (currentFile.CheckOutType == SPFile.SPCheckOutType.None && currentFile.RequiresCheckout) { currentFile.CheckOut(); checkout = true; } } SPUser suser = null; SPListItemVersion spversion = null; SPListItemVersionCollection versions = item.Versions; if (versions.Count > 0) { foreach (SPListItemVersion version in versions) { if (version.IsCurrentVersion) { suser = version.CreatedBy.User; spversion = version; item = version.ListItem; break; } } } /* * else * { * listItem = file.ListItemAllFields; * } */ using (SPSite site = new SPSite(SPContext.Current.Web.Url, suser.UserToken)) { SPWeb spweb = site.OpenWeb(); SPFile spfile = spweb.GetFile(currentFile.UniqueId); SPListItem spitem = spfile.Item.Versions.GetVersionFromLabel(spversion.VersionLabel).ListItem; PublishingPageDesignFieldValue value = spitem[BuildFieldId.PublishingPageDesign] as PublishingPageDesignFieldValue; value.Id = layout.UniqueId; value.Title = layout.Title; value.Url = layout.Url; spitem[BuildFieldId.PublishingPageDesign] = value; try { spitem.Fields[BuildFieldId.PublishingPageDesign].Update(true); } catch (Exception) { } try { spitem.Update(); } catch (Exception) { } try { spitem.SystemUpdate(false); } catch (Exception) { } try { spfile.Update(); } catch (Exception) { } } uniqueId = layout.UniqueId; //currentFile.Update(); if (checkout) { currentFile.CheckIn("Layout Changed", SPCheckinType.OverwriteCheckIn); } } } } catch (Exception ex) { ex.ToString(); //result = ex; } SPContext.Current.Web.AllowUnsafeUpdates = false; url = base.parentStateControl.ContextUri; if (IsEditMode(this.Page)) { url = WebPageStateControl.AddQueryStringParameter(WebPageStateControl.AddQueryStringParameter(url, "ControlMode", "Edit"), "DisplayMode", "Design"); } url = WebPageStateControl.AddQueryStringParameter(url, "PageLayout", DateTime.Now.Ticks.ToString()); base.parentStateControl.EnsureItemSavedIfEditMode(true); this.RefreshPageState(); this.OnPageStateChanged(); //base.ClearChildControlState(); HtmlMeta refresh = new HtmlMeta(); refresh.HttpEquiv = "refresh"; refresh.Content = "0;URL=" + url + ""; this.Page.Header.Controls.Add(refresh); this.Page.Header.Controls.Add(new LiteralControl(Environment.NewLine)); /* * if (IsEditMode(this.Page)) * { * SPUtility.Redirect("~/" + url, SPRedirectFlags.Trusted,HttpContext.Current); * } * else * { * SPUtility.Redirect("~/" + base.parentStateControl.ContextUri, SPRedirectFlags.Trusted, HttpContext.Current); * } */ }