以编程方式更改Content Editor Web部件文本
大家好,
我试图以编程方式更改shareopoint中内容编辑器Web部件的内容时遇到了一些问题.实际上,我已经在设计者的页面上创建了一个内容编辑器Web部件,当我使用下面的给定代码更改内容时.找不到错误,它显示内容已更改,但是当我尝试从内容编辑器Web部件中获取该内容时,它显示的是更改前的先前文本.因此,我的内容编辑器Web部件的内容没有更改,也没有发现错误...这是我的代码.
Hi Guys,
I am facing some problem while i am trying to change the content of content editor web part in shareopoint programmatically. Actually i have created a content editor web part on a page from designer and when i am using the below given code to change the content. No eror found and it shows that content have been changed but when i try to fetch that content from content editor web part, it is showing the previous text before getting change. So my Content editor web part content is not changing and no error found as well... Here is my code.
using (SPSite site = new SPSite(url + webPartPage))
using (SPWeb web = site.OpenWeb())
{
//give relative path of the webpartpage
SPFile file = web.GetFile(url +webPartPage);
try
{
SPLimitedWebPartManager wm = web.GetLimitedWebPartManager(webPartPage, System.Web.UI.WebControls.WebParts.PersonalizationScope.User);
if (file.CheckOutStatus == SPFile.SPCheckOutStatus.None)
{
file.CheckOut();
}
foreach (System.Web.UI.WebControls.WebParts.WebPart wp1 in wm.WebParts)
{
objUT.EnableSecurity(web, web.Site.WebApplication, site);
Microsoft.SharePoint.Administration.SPWebApplication webApp = web.Site.WebApplication;
webApp.FormDigestSettings.Enabled = false;
wp1.AllowEdit = true;
ContentEditorWebPart contentEditor = wp1 as ContentEditorWebPart;
if (contentEditor != null && wp1.ID == ddlWebpart.SelectedValue.ToString())
{
string strOldContent = contentEditor.Content.InnerText;
//Session["WPID"] = wp1.ID;
//Session["Content"] = strOldContent;
// wm.DeleteWebPart(contentEditor);
XmlDocument xmlDoc = new XmlDocument();
XmlElement xmlElement = xmlDoc.CreateElement("HtmlContent");
xmlElement.InnerText = contentEditor.Content.InnerText.Replace(strOldContent,Editor.Text); //Editor.Text;
contentEditor.Content = xmlElement;
wm.SaveChanges(contentEditor);
Editor.Text = contentEditor.Content.InnerText;
string[] ParamNames = { "PageURL", "WebpartID", "WebpartOrgContent", "WebpartChaContent", "IsApproved", "CreatedOn", "CreatedBy", "ModifiedOn", "ModifiedBy" };
string[] ParamValues = { url + webPartPage, wp1.ID, strOldContent, Editor.Text, "Pending", System.DateTime.Now.ToString(), System.Environment.UserName, System.DateTime.Now.ToString(), System.Environment.UserName };
objUT.insertWPContentUpdationHistory(url+webPartPage, ParamNames, ParamValues, strListContentUpdationHistory, strListPagesContentUpdationHistory);
}
wp1.AllowEdit = false;
webApp.FormDigestSettings.Enabled = true;
// wm.Dispose();
}
file.Update();
file.CheckIn("ss");
// Editor.Text = "";
}
catch (Exception ex1)
{
file.CheckIn('ss');
}
//Save state information of a webpart movement
}
please help....
正如您所得到的建议,请先接受一些培训,然后再尝试跳入并扔代码
As you have been advised before get some training before just trying to jump in and throw code around
using (SPSite site = new SPSite(url + webPartPage))
无需将webPartPage连接到URL,无论如何都将其忽略. SPSite是您要使用的网站集.
The is no need to concatenate the webPartPage to the URL, it is ignored anyway. SPSite is the Site Collection you are trying to use.
//give relative path of the webpartpage
SPFile file = web.GetFile(url +webPartPage);
您没有使用相对路径.您正在构建不必要的完全限定路径
You are not using a relative path. You are constructing a fully qualified path that is unnecessary
SPFile file = web.GetFile("Default.aspx");
or
SPFile file = web.GetFile("ListName/Default.aspx");
最后,Webpart并不是要以这种方式进行更新.
And finally webparts are not meant to be updated in this manner.