2010-09-01 5 views
3

Мне нужен веб-сайт, который имеет поле открытого текста для заголовка, изображение для миниатюры и блок редактирования HTML-контента, поэтому я подумал, что лучший способ сделать это - попытаться расширить существующую веб-часть Редактора контента. К сожалению, CEWP отмечен как запечатанный, поэтому я не могу его подклассифицировать. Я размышлял и пытался воссоздать функциональность в своем собственном веб-части (см. Конец вопроса для кода), но содержимое моей пользовательской версии CEWP не сохраняется.Как расширить веб-часть редактора содержимого OOTB в Sharepoint 2010?

Кто-нибудь знает, как я могу:

  • безопасно подкласс ContentEditableWebPart или
  • включают в себя богатый текстовый блок (включая RTE ленты) в пользовательских веб-части, или
  • размещения нескольких веб частей в одной «оберточной» веб-части?

Заранее благодарен!

код следует ниже (материал, который останавливал его компилляция, который был скопирован с отражателя закомментироваными/измененным/удалено)

using System; 
using System.ComponentModel; 
using System.Security.Permissions; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.HtmlControls; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Xml; 
using System.Xml.Serialization; 
using Microsoft.SharePoint; 
using Microsoft.SharePoint.Security; 
using Microsoft.SharePoint.Utilities; 
using Microsoft.SharePoint.WebControls; 
using Microsoft.SharePoint.WebPartPages; 

namespace Public.Webparts 
{ 
    [ToolboxItemAttribute(false)] 
    [XmlRoot(Namespace="Webparts/ProductItem")] 
    public class ProductItemWebPart :System.Web.UI.WebControls.WebParts.WebPart 
    { 


    // Fields 
    private string _content; 
    private bool _contentHasToken; 
    private string _contentLink; 
    private string _partContent; 
    private string _partStorage; 
    private HtmlGenericControl editableRegion = new HtmlGenericControl(); 
    private HtmlGenericControl emptyPanel = new HtmlGenericControl(); 
    private const string EmptyPanelHtmlV4 = "<A href=\"#\" title=\"{0}\" style=\"padding:8px 0px\" class=\"ms-toolbar ms-selectorlink\" >{0}</A>"; 
    internal const string InputContentClientId = "content"; 

    // Methods 
    [AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal)] 
    public ProductItemWebPart() 
    { 
     //base.PreRender += new EventHandler(this.OnPreRender); 
     if (this.Title.Length == 0) 
     { 
      this.Title = "Product Item Webpart"; 
     } 
     if (this.Description.Length == 0) 
     { 
      this.Description = "This web part contains a title, content and image for a product item."; 
     } 
    } 

    private string GetContent() 
    { 

     if (this._partContent != null) 
     { 
      return SPHttpUtility.NoEncode(ReplaceTokens(this._partContent)); 
     } 
     return ""; 
    } 

    protected internal string ReplaceTokens(string input) 
    { 
     string str = string.Empty; 
     if (this.WebPartManager != null) 
     { 
      return SPWebPartManager.ReplaceTokens(HttpContext.Current, SPContext.Current.Web, this, input); 
     } 
     if (input != null) 
     { 
      str = input; 
     } 
     return str; 
    } 


    private string getEmptyPanelHtml() 
    { 
     return "<A href=\"#\" title=\"Click to enter content.\" style=\"padding:8px 0px\" class=\"ms-toolbar ms-selectorlink\" >Click to enter content.</A>"; 
    } 



    //private void HttpAsyncCallback(object state) 
    //{ 
    // ULS.SendTraceTag(0x38393969, ULSCat.msoulscat_WSS_WebParts, ULSTraceLevel.Medium, "ASYNC: Http Callback: UniqueID={0}", new object[] { this.UniqueID.ToString() }); 
    // if ((HttpStatusCode.OK != base.GetHttpWebResponse(this._contentLink, out this._partContent)) && (this._content == null)) 
    // { 
    //  this._partContent = "<p class=\"UserGeneric\">" + SPHttpUtility.HtmlEncode(WebPartPageResource.GetString("CannotRetrieveContent", new object[] { WebPartPageResource.GetString("ContentLinkLiteral") })) + "</p>"; 
    // } 
    //} 

    private bool inEditMode() 
    { 
     SPWebPartManager currentWebPartManager = (SPWebPartManager) WebPartManager.GetCurrentWebPartManager(this.Page); 
     return (((currentWebPartManager != null) && !base.IsStandalone) && currentWebPartManager.GetDisplayMode().AllowPageDesign); 
    } 

    //[SharePointPermission(SecurityAction.Demand, ObjectModel=true)] 
    //public override string LoadResource(string id) 
    //{ 
    // string str = WebPartPageResource.GetString(id); 
    // if (str != null) 
    // { 
    //  return str; 
    // } 
    // return id; 
    //} 

    [SharePointPermission(SecurityAction.Demand, ObjectModel=true)] 
    protected override void OnInit(EventArgs e) 
    { 
     base.OnInit(e); 
     if (this.ShowContentEditable()) 
     { 
      SPRibbon current = SPRibbon.GetCurrent(this.Page); 
      if (current != null) 
      { 
       current.MakeTabAvailable("Ribbon.EditingTools.CPEditTab"); 
       current.MakeTabAvailable("Ribbon.Image.Image"); 
       current.MakeTabAvailable("Ribbon.EditingTools.CPInsert"); 
       current.MakeTabAvailable("Ribbon.Link.Link"); 
       current.MakeTabAvailable("Ribbon.Table.Layout"); 
       current.MakeTabAvailable("Ribbon.Table.Design"); 
       //if (!(this.Page is WikiEditPage)) 
       //{ 
       // current.TrimRTEWikiControls(); 
       //} 
      } 
     } 
    } 

    [SharePointPermission(SecurityAction.Demand, ObjectModel=true)] 
    protected override void OnLoad(EventArgs e) 
    { 
     this.Controls.Add(this.editableRegion); 
     this.Controls.Add(this.emptyPanel); 
     this.editableRegion.Visible = false; 
     this.emptyPanel.Visible = false; 
     base.OnLoad(e); 
     string str = this.Page.Request.Form[this.ClientID + "content"]; 
     if ((str != null) && (this._content != str)) 
     { 
      this._content = str; 
      try 
      { 
       SPWebPartManager currentWebPartManager = (SPWebPartManager) WebPartManager.GetCurrentWebPartManager(this.Page); 
       Guid storageKey = currentWebPartManager.GetStorageKey(this); 
       currentWebPartManager.SaveChanges(storageKey); 
      } 
      catch (Exception exception) 
      { 
       Label child = new Label(); 
       child.Text = exception.Message; 
       this.Controls.Add(child); 
      } 
     } 
     if (this.ShowContentEditable()) 
     { 
      string str2; 
      //if (this.ContentHasToken) 
      //{ 
      // str2 = ReplaceTokens(this._content); 
      //} 
      //else 
      //{ 
      // str2 = this._content; 
      //} 

      str2 = this._content; 

      this.Page.ClientScript.RegisterHiddenField(this.ClientID + "content", str2); 
      this.editableRegion.Visible = true; 
      this.emptyPanel.Visible = true; 
      this.emptyPanel.TagName = "DIV"; 
      this.emptyPanel.Style.Add(HtmlTextWriterStyle.Cursor, "hand"); 
      this.emptyPanel.Controls.Add(new LiteralControl(this.getEmptyPanelHtml())); 
      this.emptyPanel.Style.Add(HtmlTextWriterStyle.TextAlign, "center"); 
      base.Attributes["RteRedirect"] = this.editableRegion.ClientID; 
      ScriptLink.RegisterScriptAfterUI(this.Page, "SP.UI.Rte.js", false); 
      ScriptLink.RegisterScriptAfterUI(this.Page, "SP.js", false); 
      ScriptLink.RegisterScriptAfterUI(this.Page, "SP.Runtime.js", false); 
      this.editableRegion.TagName = "DIV"; 
      this.editableRegion.InnerHtml = str2; 
      this.editableRegion.Attributes["class"] = "ms-rtestate-write ms-rtestate-field"; 
      this.editableRegion.Attributes["contentEditable"] = "true"; 
      this.editableRegion.Attributes["InputFieldId"] = this.ClientID + "content"; 
      this.editableRegion.Attributes["EmptyPanelId"] = this.emptyPanel.ClientID; 
      this.editableRegion.Attributes["ContentEditor"] = "True"; 
      this.editableRegion.Attributes["AllowScripts"] = "True"; 
      this.editableRegion.Attributes["AllowWebParts"] = "False"; 
      string script = "RTE.RichTextEditor.transferContentsToInputField('" + SPHttpUtility.EcmaScriptStringLiteralEncode(this.editableRegion.ClientID) + "');"; 
      this.Page.ClientScript.RegisterOnSubmitStatement(base.GetType(), "transfer" + this.editableRegion.ClientID, script); 
      if (string.IsNullOrEmpty(this._content)) 
      { 
       this.emptyPanel.Style["display"] = ""; 
       this.editableRegion.Style["display"] = "none"; 
      } 
      else 
      { 
       this.emptyPanel.Style["display"] = "none"; 
       this.editableRegion.Style["display"] = ""; 
      } 
     } 
    } 

    //private void OnPreRender(object sender, EventArgs e) 
    //{ 
    // Uri fullURLPath = base.GetFullURLPath(this._contentLink); 
    // if ((fullURLPath != null) && !base.TryToGetFileFromDatabase(fullURLPath, out this._partContent)) 
    // { 
    //  ULS.SendTraceTag(0x38393968, ULSCat.msoulscat_WSS_WebParts, ULSTraceLevel.Medium, "ASYNC: Begin Fetch: UniqueID={0}", new object[] { this.UniqueID.ToString() }); 
    //  base.RegisterWorkItemCallback(new WaitCallback(this.HttpAsyncCallback), null); 
    // } 
    //} 

    [SharePointPermission(SecurityAction.Demand, ObjectModel=true)] 
    //protected override void RenderWebPart(HtmlTextWriter writer) 
    protected override void Render(HtmlTextWriter writer) 
    { 
     //if (this.ShowContentEditable() && base.WebPartManager.IsAllowedToScript(this)) 
     if (this.ShowContentEditable()) 
     { 
      base.Render(writer); 
     } 
     else 
     { 
      writer.Write(this.GetContent()); 
     } 
    } 

    //[SharePointPermission(SecurityAction.Demand, ObjectModel=true)] 
    //protected internal override bool RequiresWebPartClientScript() 
    //{ 
    // return true; 
    //} 

    //public bool ShouldSerializeContent() 
    //{ 
    // if (!base.SerializeAll) 
    // { 
    //  return (this.WebPartDefault._content != this._content); 
    // } 
    // return true; 
    //} 

    //public bool ShouldSerializeContentHasToken() 
    //{ 
    // return ((this.WebPartDefault._contentHasToken != this._contentHasToken) && !base.SerializeAll); 
    //} 

    //public bool ShouldSerializeContentLink() 
    //{ 
    // if (!base.SerializeAll) 
    // { 
    //  return (this.WebPartDefault._contentLink != this._contentLink); 
    // } 
    // return true; 
    //} 

    public bool ShouldSerializePartStorage() 
    { 
     //if (!base.SerializeAll) 
     //{ 
      //return (this.WebPartDefault._partStorage != this._partStorage); 
     //} 
     return true; 
    } 

    internal bool ShowContentEditable() 
    { 
     return (((SPContext.Current.Web.UIVersion > 3) && (this._partContent == null)) && this.inEditMode()); 
    } 


    //[XmlElement("ContentHasToken", IsNullable=false), Browsable(false), WebPartStorage(Storage.Shared)] 
    //public bool ContentHasToken 
    //{ 
    // get 
    // { 
    //  return this._contentHasToken; 
    // } 
    // set 
    // { 
    //  this._contentHasToken = value; 
    // } 
    //} 



    //private string EncodedInvalidContentError 
    //{ 
    // get 
    // { 
    //  string str2; 
    //  string str = "<a id=\"" + this.ID + "HelpLink\" href=\"javascript:HelpWindowUrl('" + SPHttpUtility.NoEncode("sts/html/dpvwpabt.htm") + "');\">" + SPHttpUtility.HtmlEncode(WebPartPageResource.GetString("InvalidContentErrorHelpLink")) + "</a>"; 
    //  if ((this.Context != null) && Utility.BrowserIsIE(this.Context.Request.Browser)) 
    //  { 
    //   str2 = "InvalidContentError"; 
    //  } 
    //  else 
    //  { 
    //   str2 = "InvalidContentErrorDL"; 
    //  } 
    //  return ("<p><div class=\"UserGeneric\">" + string.Format(CultureInfo.InvariantCulture, SPHttpUtility.HtmlEncodeAllowSimpleTextFormatting(WebPartPageResource.GetString(str2)), new object[] { str }) + "</div></p>"); 
    // } 
    //} 

    [Resources("PartStorageLiteral", "Advanced", "PartStorage"), WebPartStorage(Storage.Personal), XmlElement("PartStorage", IsNullable=false)] 
    public string PartStorage 
    { 
     get 
     { 
      //return Utility.GetMemberString(this._partStorage); 
      return this._partStorage ?? String.Empty; 
     } 
     set 
     {   
      //Utility.SetMemberString(ref this._partStorage, value); 

      if (value != null && value.Length > 0) 
      { 
       this._partStorage = value; 
      } 
      else 
      { 
       this._partStorage = null; 
      } 
     } 
    } 

    //internal ContentEditorWebPart WebPartDefault 
    //{ 
    // get 
    // { 
    //  return (ContentEditorWebPart) base.WebPartDefault; 
    // } 
    //} 


    } 
} 
+0

может вы описать, что вы пытаетесь выполнить немного больше? Наверное, мне интересно, почему вы не можете использовать один из предоставленных макетов страниц, у которого уже есть заголовок, изображение, блок html ИЛИ создать свой собственный макет страницы. Это выглядит намного лучше, чем текущий путь, который вы преследуете, но, может быть, я что-то упустил. – nelsonwebs

+0

Причина, по которой мы не собираемся использовать макеты страниц, состоит в том, что существует множество «блоков контента» с заданным макетом, которые нужно переходить на страницу, и мы хотели пойти с пользовательской веб-частью над чем-то вроде CQWP или DVWP, чтобы упростить переупорядочивание блоков контента. Хотя мы могли бы просто создавать макеты страниц для типа содержимого с большим количеством необязательных полей (RollupImage1, RollupTitle1, RollupText1, RollupImage2, RollupTitle2), это не будет очень хорошим опытом создания контента. Пожалуйста, посмотрите эту удивительную диаграмму (lol) для более подробной информации - http://yfrog.com/mr90058291p –

+0

И я хочу расширить CEWP (в отличие от создания веб-страницы с нуля), чтобы использовать его богатую интеграцию с текстом и лентой. –

ответ

5

Думаю, что это может решить вашу проблему - вы на правильном пути, когда вы получили Отражатель из :)

http://zootfroot.blogspot.com/2010/09/develop-custom-editable-visual-web-part.html

+0

Awesome awesome awesome! Большое спасибо ... У меня будет игра, но из начальных тестов это выглядит очень хорошо! –

+0

-1 для ссылки на ссылку. Сожалею. –

+1

Правда, но это была ссылка на MY post, которая установила контекст, прежде чем связываться с моим решением. Мой ответ, должно быть, был полезен, поскольку OP принял его как лучший ответ. –

0

Почему бы не создать «Visual Web Part» и используйте богатый текстовый редактор (например, Telerik или другие)? Я думаю, что попытка перепроектировать код для CEWP, вероятно, будет чрезмерной и, вероятно, против лицензии SharePoint.

+1

Использование поля с богатым текстовым редактором означало бы также подключение его к ленте, что я не совсем уверен, как это сделать. –

+0

Я согласен с Blakomen здесь. Редактор содержимого OOTB выглядит круто в SharePoint 2010 и предоставляет интерфейс с MS Office с лентой. И я не уверен, что я могу легко переделать эту веб-часть, потому что таким образом мне нужно интегрировать существующий редактор WISYWIG в ленту. –

1

Вы можете использовать ControlAdapter, как описано в этой статье: http://blog.mastykarz.nl/inconvenient-content-editor-web-part/

+0

Это лучший вариант до сих пор, хотя он и включает изменения в web.config. –

+0

Вам не нужно менять файл web.config. * .browser хранится в папке веб-приложения IIS с именем «App_Browsers». Вы можете перейти на C: \ inetpub \ wwwroot \ wss \ VirtualDirectories \ 80 \ App_Browsers, там вы можете увидеть файл OOTB, compat.browser.Согласно статье, вы должны разместить свой собственный файл * .browser. Я сделал это с помощью пользовательского TimerJob, который запускается, тогда моя функция активируется. Я могу предоставить вам код, если вам нужно. –

Смежные вопросы