以编程方式更改Content Editor Web部件文本 [英] Changing Content Editor Web part Text Programmatically

查看:106
本文介绍了以编程方式更改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.


这篇关于以编程方式更改Content Editor Web部件文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆