gpt4 book ai didi

c# - 如何在 Sharepoint 2010 中扩展 OOTB 内容编辑器 Web 部件?

转载 作者:行者123 更新时间:2023-11-30 14:41:53 25 4
gpt4 key购买 nike

我需要一个 Web 部件,它有一个用于标题的纯文本字段、一个用于缩略图的图像和一个 HTML 内容可编辑 block ,所以我认为最好的方法是尝试扩展现有的内容编辑器 Web 部件.不幸的是,CEWP 被标记为密封的,所以我不能将它子类化。我已经反射(reflect)并尝试在我自己的自定义 Web 部件中重新创建功能(请参阅问题末尾的代码),但我的自定义版本的 CEWP 的内容不会保留。

有谁知道我该怎么做:

  • 安全地子类化 ContentEditableWebPart,或者
  • 在自定义 Web 部件中包含富文本 block (包括 RTE 功能区),或者
  • 在一个“包装器”Web 部件中托管多个 Web 部件?

提前致谢!

代码如下(从反射器复制的阻止它编译的东西已被注释掉/更改/删除)

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;
// }
//}


}
}

最佳答案

认为这可能会解决您的问题 - 当您推出 Reflector 时,您走在了正确的轨道上 :)

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

关于c# - 如何在 Sharepoint 2010 中扩展 OOTB 内容编辑器 Web 部件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3614989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com