public Comment(SPListItem spli, int postId) { try { this.CommentID = (int)spli[HelperConst.ID]; this.PostID = (int)spli[HelperConst.PostID]; this.Title = spli.GetFormattedValue(HelperConst.Tittle); this.Comtents = spli.GetFormattedValue(HelperConst.CommentContent); this.DateCreated = (DateTime)spli[HelperConst.DateModified]; } catch (Exception ex) { throw ex; } }
public Post(SPListItem spli, SPWeb web) { try { if (spli != null) { this.Title = spli.GetFormattedValue(HelperConst.Tittle); this.PostID = int.Parse(spli.GetFormattedValue(HelperConst.ID)); this.Content = spli.GetFormattedValue(HelperConst.PostContent); this.PublishedDate = (DateTime)spli[HelperConst.PostPublishedDate]; this.DateModified = (DateTime)spli[HelperConst.DateModified]; this.Author = spli.GetFormattedValue(HelperConst.Author); //need test this.PostURL = new SPFieldUrlValue(spli[HelperConst.URL].ToString()).Url; } } catch (Exception ex) { throw ex; } }
/* * private SPListItem _Author; * * public SPListItem Author * { * get { return _Author; } * set { _Author = value; } * } * * private SPListItem _Editor; * * public SPListItem Editor * { * get { return _Editor; } * set { _Editor = value; } * } */ protected override void OnInit(EventArgs e) { base.OnInit(e); try { SPContext _current = SPContext.Current; if (_current == null) { _current = SPContext.GetContext(HttpContext.Current); } settings = new MetaDataSettings(); settings = settings.Load(_current.Site); facebook = new FaceBookSettings(); facebook = facebook.Load(_current.Site); twitter = new TwitterSettings(); twitter = twitter.Load(_current.Site); listItem = _current.ListItem; /* * try * { * SPSecurity.RunWithElevatedPrivileges(delegate() * { * SPList userList = _current.Site.RootWeb.SiteUserInfoList; * * if (userList != null && listItem != null) * { * SPFieldUserValue userValue = new SPFieldUserValue(_current.Web, listItem[SPBuiltInFieldId.Editor].ToString()); * SPUser editor = userValue.User; * Editor = userList.Items.GetItemById(editor.ID); * * userValue = new SPFieldUserValue(_current.Web, listItem[SPBuiltInFieldId.Author].ToString()); * SPUser author = userValue.User; * Author = userList.Items.GetItemById(editor.ID); * } * }); * } * catch { } */ GetMetaFields(); if (HttpContext.Current.User != null && HttpContext.Current.User.Identity.IsAuthenticated) { UpdateMetaFields(); } if (_current.List == null) { HtmlMeta compatible = new HtmlMeta(); compatible.HttpEquiv = "X-UA-Compatible"; compatible.Content = "IE=edge,chrome=1"; this.Control.Controls.Add(compatible); this.Control.Controls.Add(new LiteralControl(Environment.NewLine)); } else { string editform = _current.List.Forms[PAGETYPE.PAGE_EDITFORM].ServerRelativeUrl; //string editdialogform = SPContext.Current.List.Forms[PAGETYPE.PAGE_EDITFORMDIALOG].ServerRelativeUrl; string newform = _current.List.Forms[PAGETYPE.PAGE_NEWFORM].ServerRelativeUrl; //string newdialogform = SPContext.Current.List.Forms[PAGETYPE.PAGE_NEWFORMDIALOG].ServerRelativeUrl; bool isEdit = Page.Request.Url.OriginalString.Contains(editform); bool isNew = Page.Request.Url.OriginalString.Contains(newform); bool isSharePresence = false; if (listItem != null && listItem.ContentType != null && listItem.ContentType.Id.IsChildOf(ContentTypeId.PageTemplate)) { isSharePresence = true; } if (isSharePresence) { this.Control.Controls.Add(new LiteralControl(Environment.NewLine)); this.Control.Controls.Add(new LiteralControl("<!-- Managed MetaData -->")); this.Control.Controls.Add(new LiteralControl(Environment.NewLine)); HtmlMeta compatible = new HtmlMeta(); compatible.HttpEquiv = "X-UA-Compatible"; DisplayControlModes modes = WebSitePage.DetermineDisplayControlModes(); if (modes.displayMode == SPControlMode.Display && (!isNew && !isEdit)) { //compatible.Content = "IE=10"; compatible.Content = "IE=edge,chrome=1"; } else { if (isNew && isEdit) { compatible.Content = "IE=8"; } else { compatible.Content = "IE=edge,chrome=1"; } } this.Control.Controls.Add(compatible); this.Control.Controls.Add(new LiteralControl(Environment.NewLine)); /* * string googleId = Editor["Google_x0020_Id"] as string; * string twitterId = Editor["Twitter_x0020_Id"] as string; * string linkedinId = Editor["LinkedIn_x0020_Id"] as string; * string facebookId = Editor["FaceBook_x0020_Id"] as string; */ if (SPContext.Current.FormContext.FormMode == SPControlMode.Display) { if (settings.UseMSNBotDMOZ) { HtmlMeta msnbot = new HtmlMeta(); msnbot.Name = "msnbot"; msnbot.Content = "NOODP"; this.Control.Controls.Add(msnbot); this.Control.Controls.Add(new LiteralControl(Environment.NewLine)); } /* * if (!string.IsNullOrEmpty(Editor["Google_x0020_Id"] as string)) * { * HtmlLink googleplus = new HtmlLink(); * googleplus.Attributes.Add(HtmlTextWriterAttribute.Rel.ToString().ToLower(), "author"); * googleplus.Attributes.Add(HtmlTextWriterAttribute.Href.ToString().ToLower(), string.Format("https://plus.google.com/{0}/posts", Editor["Google_x0020_Id"] as string)); * this.Control.Controls.Add(googleplus); * this.Control.Controls.Add(new LiteralControl(Environment.NewLine)); * } */ Regex mapping = new Regex(@"\[(.*?)\]", RegexOptions.Compiled); foreach (SPField oField in metafields) { string fieldname = SharePointWebControls.GetFieldName(oField); if (listItem.Fields.ContainsField(fieldname)) { string value = listItem.GetFormattedValue(fieldname);// field.Title); //string value = listItem[fieldname] as string; HtmlMeta meta = new HtmlMeta(); meta.Name = fieldname; if (String.IsNullOrEmpty(value)) { value = oField.DefaultValue; //meta.Content = oField.DefaultValue; } else { value = value.Replace(";#", ","); //meta.Content = value; } if (!string.IsNullOrEmpty(value) && mapping.IsMatch(value)) { string shallow = value.ToString(); foreach (Match match in mapping.Matches(shallow)) { string field = match.Value; string sfield = field.Trim(new char[2] { '[', ']' }); if (listItem.Fields.ContainsField(sfield)) { //string fieldvalue = listItem.GetFormattedValue(sfield); string fieldvalue = listItem[sfield] as string; if (string.IsNullOrEmpty(fieldvalue)) { try { SPField internalfield = listItem.Fields.GetFieldByInternalName(sfield); if (internalfield != null) { fieldvalue = internalfield.DefaultValue as string; } //fieldvalue = listItem.Fields[sfield].DefaultValue; } catch { } } if (sfield.ToLower().Contains("author") || sfield.ToLower().Contains("editor") || sfield.ToLower().Contains("publisher")) { /* * if (sfield.ToLower().Contains("author")) * { * fieldvalue = Author["Title"] as string; * } * else * { * fieldvalue = Editor["Title"] as string; * } */ /* * string[] sections = fieldvalue.Split(new string[] { ";#" }, StringSplitOptions.RemoveEmptyEntries); * if (sections.Length > 1) * { * fieldvalue = sections[1]; * } */ //meta.Content = value; } if (sfield.ToLower().Contains("html5")) { if (sfield.ToLower().Contains("image")) { HTML5Image image = listItem.Fields.GetFieldByInternalName(sfield) as HTML5Image; HTML5ImageField ifield = new HTML5ImageField(image.FieldRenderingControl.Value.ToString()); fieldvalue = listItem.Web.Site.Url + "/" + ifield.Src; } if (sfield.ToLower().Contains("header")) { HTML5Header header = listItem.Fields.GetFieldByInternalName(sfield) as HTML5Header; HTML5HeaderField ifield = new HTML5HeaderField(header.FieldRenderingControl.Value.ToString()); fieldvalue = ifield.Text; } if (sfield.ToLower().Contains("footer")) { HTML5Footer footer = listItem.Fields.GetFieldByInternalName(sfield) as HTML5Footer; HTML5FooterField ifield = new HTML5FooterField(footer.FieldRenderingControl.Value.ToString()); fieldvalue = ifield.Text; } if (sfield.ToLower().Contains("video")) { HTML5Video video = listItem.Fields.GetFieldByInternalName(sfield) as HTML5Video; HTML5VideoField ifield = new HTML5VideoField(video.FieldRenderingControl.Value.ToString()); fieldvalue = listItem.Web.Site.Url + "/" + ifield.Src; } //fieldvalue = fieldvalue.Replace("//", "/"); } value = value.Replace(field, fieldvalue); } /* * if (sfield.Contains("Twitter_x0020_Id")) * { * value = Editor["Twitter_x0020_Id"] as string; * } * * if (sfield.Contains("Google_x0020_Id")) * { * value = Editor["Google_x0020_Id"] as string; * } * * if (sfield.Contains("FaceBook_x0020_Id")) * { * value = Editor["FaceBook_x0020_Id"] as string; * } * * if (sfield.Contains("LinkedIn_x0020_Id")) * { * value = Editor["LinkedIn_x0020_Id"] as string; * } */ if (sfield.ToLower().Contains("site")) { value = listItem.Web.Site.Url + "/"; } if (sfield.ToLower().Contains("url")) { value = HttpContext.Current.Request.Url.OriginalString; //value = listItem.Web.Site.Url + "/" + listItem.Url; } } } meta.Content = value; if (fieldname == "keywords") { if (settings.AutoKeywords) { if (String.IsNullOrEmpty(value)) { value = String.Empty; } meta.Content += GetAutoKeywords(value); } } if (fieldname == "language" && settings.UseSiteLanguage) { CultureInfo cult = new CultureInfo((int)SPContext.Current.Web.RegionalSettings.LocaleId); //CultureInfo cult = new CultureInfo((int)SPContext.Current.Web.Language); meta.Content = cult.DisplayName.ToLower(); HtmlMeta equiv = new HtmlMeta(); equiv.HttpEquiv = "content-language"; equiv.Content = cult.Name; this.Control.Controls.Add(equiv); this.Control.Controls.Add(new LiteralControl(Environment.NewLine)); equiv = new HtmlMeta(); equiv.HttpEquiv = "language"; equiv.Content = cult.Name; this.Control.Controls.Add(equiv); this.Control.Controls.Add(new LiteralControl(Environment.NewLine)); } /* * if (fieldname == "author" && settings.AuthorOverride != Authors.NoOverride) * { * value = listItem[settings.AuthorOverride.ToString()] as string; * meta.Content = value; * } * * if (fieldname == "web_author" && settings.WebAuthorOverride != Authors.NoOverride) * { * value = listItem[settings.AuthorOverride.ToString()] as string; * meta.Content = value; * } */ this.Control.Controls.Add(meta); this.Control.Controls.Add(new LiteralControl(Environment.NewLine)); } } this.Control.Controls.Add(new LiteralControl(Environment.NewLine)); } else { HtmlMeta pragma = new HtmlMeta(); pragma.HttpEquiv = "Pragma"; pragma.Content = "no-cache"; this.Control.Controls.Add(pragma); this.Control.Controls.Add(new LiteralControl(Environment.NewLine)); HtmlMeta expires = new HtmlMeta(); expires.HttpEquiv = "Expires"; expires.Content = "-1"; this.Control.Controls.Add(expires); this.Control.Controls.Add(new LiteralControl(Environment.NewLine)); HtmlMeta store = new HtmlMeta(); store.HttpEquiv = "Cache-Control"; store.Content = "no-store, no-cache, must-revalidate"; this.Control.Controls.Add(store); this.Control.Controls.Add(new LiteralControl(Environment.NewLine)); HtmlMeta post = new HtmlMeta(); post.HttpEquiv = "Cache-Control"; post.Content = "post-check=0, pre-check=0"; this.Control.Controls.Add(post); this.Control.Controls.Add(new LiteralControl(Environment.NewLine)); } } } } catch (Exception ex) { ex.ToString(); } }
protected void update_summary(SPItemEventProperties properties) { SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite site = new SPSite(properties.SiteId)) { using (SPWeb web = site.OpenWeb()) { string summary = null, image_url = null, title = null, type = null; try { SPListItem page = properties.ListItem; if (page.GetFormattedValue("_ModerationStatus") != "已批准") { delete_summary(properties); return; } string content = page["Wiki 内容"].ToString(); XmlDocument xml_doc = new XmlDocument(); xml_doc.LoadXml(content); XmlNode node = xml_doc.SelectSingleNode("descendant::div[contains(@class,'ms-rteElement-Callout2')]"); if (node != null) { summary = node.InnerText; } node = xml_doc.SelectSingleNode("descendant::img[contains(@class,'ms-rteImage-2')]"); if (node != null) { image_url = node.Attributes["src"].Value; } node = xml_doc.SelectSingleNode("descendant::h3[contains(@class,'ms-rteElement-H3B')]"); if (node != null) { title = node.InnerText; } node = xml_doc.SelectSingleNode("descendant::div[contains(@class,'ms-rteElement-Callout1')]"); if (node != null) { type = node.InnerText; } } catch (Exception ex) { log(site, "Wiki Page ID:" + properties.ListItemId + " 解析内容时发生错误。", "错误", ex.ToString()); } if (summary != null && image_url != null && title != null && type != null) { try { SPList summary_list = web.Lists["新闻摘要"]; SPQuery query = new SPQuery(); query.Query = "<Where><Eq><FieldRef Name='_x65b0__x95fb_ID' /><Value Type='Text'>" + properties.ListItemId + "</Value></Eq></Where>"; SPListItem summary_item = null; SPListItemCollection summary_items = summary_list.GetItems(query); if (summary_items != null && summary_items.Count > 0) { summary_item = summary_items[0]; } else { summary_item = summary_list.AddItem(); } summary_item["Title"] = title; summary_item["图片"] = image_url; summary_item["摘要"] = summary; summary_item["分类"] = type; summary_item["新闻链接"] = properties.ListItem.Url; summary_item["新闻ID"] = properties.ListItemId; summary_item.Update(); log(site, "Wiki Page ID:" + properties.ListItemId + " 【" + title + "】摘要已经更新。", "消息", "摘要更新完成。"); } catch (Exception ex) { if (site != null) { log(site, "Wiki Page ID:" + properties.ListItemId + " 写入新闻摘要时出错。", "警告", ex.ToString()); } } } else { log(site, "Wiki Page ID:" + properties.ListItemId + " 未能包含足够的新闻要素。", "警告", "未能包含足够的新闻要素"); } } } }); }
private void CreateEditorFields() { if (Fields != null && metafields != null) { foreach (SPField field in metafields) { if (listitem.Fields.ContainsField(field.InternalName)) { string value = listitem.GetFormattedValue(field.Title); Html5.WebControls.Label label = new Html5.WebControls.Label(); label.Title = field.Title; label.Class = "required"; label.For = "previous"; label.InnerText = field.Title; Fields.Controls.Add(label); if (!string.IsNullOrEmpty(field.Description)) { Html5.WebControls.Label description = new Html5.WebControls.Label(); description.Title = field.Title; description.Class = "required"; description.For = "previous"; description.InnerText = field.Description; Fields.Controls.Add(description); } if (string.IsNullOrEmpty(value)) { value = field.DefaultValue; } WebControl FieldControl = null; //SPFieldMultiLineText if (field.Type == SPFieldType.Text) { var text = (SPFieldText)field; Html5.WebControls.TextInput input = new Html5.WebControls.TextInput(); input.ID = field.Id.ToString();; input.Class = "required"; input.For = "previous"; input.TextMode = TextBoxMode.SingleLine; input.Value = value; FieldControl = input; } //SPFieldText if (field.Type == SPFieldType.Note) { var note = (SPFieldMultiLineText)field; Html5.WebControls.TextInput input = new Html5.WebControls.TextInput(); input.ID = field.Id.ToString(); input.Class = "required"; input.For = "previous"; input.Rows = note.NumberOfLines; input.TextMode = TextBoxMode.MultiLine; input.Value = value; FieldControl = input; } if (field.Type == SPFieldType.Choice) { var choice = (SPFieldChoice)field; if (choice.FieldRenderingControl.GetType() == typeof(Microsoft.SharePoint.WebControls.CheckBoxChoiceField)) { CheckBoxList input = new CheckBoxList(); foreach (String achoice in choice.Choices) { ListItem item = new ListItem(achoice, achoice, true); if (value.Contains(achoice)) { item.Selected = true; } input.Items.Add(item); } input.ID = field.Id.ToString(); //input.SelectedValue = value; FieldControl = input; } if (choice.FieldRenderingControl.GetType() == typeof(Microsoft.SharePoint.WebControls.RadioButtonChoiceField)) { RadioButtonList input = new RadioButtonList(); foreach (String achoice in choice.Choices) { ListItem item = new ListItem(achoice, achoice, true); input.Items.Add(item); } input.ID = field.Id.ToString(); input.SelectedValue = value; FieldControl = input; } if (choice.FieldRenderingControl.GetType() == typeof(Microsoft.SharePoint.WebControls.DropDownChoiceField)) { DropDownList input = new DropDownList(); foreach (String achoice in choice.Choices) { ListItem item = new ListItem(achoice, achoice, true); input.Items.Add(item); } input.ID = field.Id.ToString(); input.SelectedValue = value; FieldControl = input; } } if (field.Type == SPFieldType.MultiChoice) { var choice = (SPFieldMultiChoice)field; if (choice.FieldRenderingControl.GetType() == typeof(Microsoft.SharePoint.WebControls.CheckBoxChoiceField)) { CheckBoxList input = new CheckBoxList(); foreach (String achoice in choice.Choices) { ListItem item = new ListItem(achoice, achoice, true); if (value.Contains(achoice)) { item.Selected = true; } input.Items.Add(item); } input.ID = field.Id.ToString(); input.SelectedValue = value; FieldControl = input; } if (choice.FieldRenderingControl.GetType() == typeof(Microsoft.SharePoint.WebControls.RadioButtonChoiceField)) { RadioButtonList input = new RadioButtonList(); foreach (String achoice in choice.Choices) { ListItem item = new ListItem(achoice, achoice, true); input.Items.Add(item); } input.ID = field.Id.ToString(); input.SelectedValue = value; FieldControl = input; } if (choice.FieldRenderingControl.GetType() == typeof(Microsoft.SharePoint.WebControls.DropDownChoiceField)) { DropDownList input = new DropDownList(); foreach (String achoice in choice.Choices) { ListItem item = new ListItem(achoice, achoice, true); input.Items.Add(item); } input.ID = field.Id.ToString(); input.SelectedValue = value; FieldControl = input; } } if (field.Type == SPFieldType.Number) { var number = (SPFieldNumber)field; Html5.WebControls.NumberInput input = new Html5.WebControls.NumberInput(); input.ID = field.Id.ToString(); FieldControl = input; } if (FieldControl != null) { Fields.Controls.Add(FieldControl); } } } } }