开发者

How to list and edit html of all web part in sharepoint 2007?

I am new in sharepoint and I have to do a very simple modification in all web part. We have lots of web part containing very simple html. The html only contain a link and an image.

Web developers had put full links to pages and images and it cause some problems. I want to scan all of the web parts html and replace full links by relative links.

Is it possible ? We have tons of pages and links. Doing it manually will take 2 weeks!!!

Thanks!

EDIT #2: Now the question is: Is it possible to list all aspx files in my website? I know how to access the web parts content with a url :

using (SPLimitedWebPartManager manager = web.GetLimitedWebPartManager(
                "ca/Pages/Home.a开发者_如何学Gospx", PersonalizationScope.Shared))
        {
            foreach (System.Web.UI.WebControls.WebParts.WebPart wp in manager.WebParts)
            {
                System.Console.WriteLine(wp.Title);
                if (wp.GetType().Equals(typeof(Microsoft.SharePoint.WebPartPages.ContentEditorWebPart)))
                {
                    Microsoft.SharePoint.WebPartPages.ContentEditorWebPart thisWebPart = wp as Microsoft.SharePoint.WebPartPages.ContentEditorWebPart;

                    System.Console.WriteLine(thisWebPart.Content.InnerText );
                    System.Console.WriteLine(thisWebPart.Content.InnerXml);
                }

            }
        }

EDIT #1: As requested their is an example:

I want to remove "http://www.mywebsite.com" from all shared webparts with code like this:

<A title="" href="http://www.mywebsite.com/Pages/Career.aspx" target=""><IMG style="BORDER-RIGHT: 0px solid; BORDER-TOP: 0px solid; BORDER-LEFT: 0px solid; BORDER-BOTTOM: 0px solid" src="http://www.mywebsite.com/images/Career.jpg" border=0></A>


In content editor web part the content is stored under content tag

<Content xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor"><![CDATA[<p>test document test document</p>]]></Content>

what i can suggest here is to open the site in sharepoint desginer and use the find and replace option for all pages


I am not sure if I got what's required exactly. What about writing a program that do that? Detect the links by regex and replace them.


If you want to actually change the content in Sharepoint, it could be difficult to do this in code. Every webpart works differently, so there's no standard solution for all web parts. For example, a CQWP can pull data from various lists, so the way to make the change for that webpart would be to change the data in the lists it pulls from. Other webparts might pull data from SQL Server, Reports, have IFrames in them, etc. Some webparts might even have the URLs hardcoded in a custom DLL, which you could only change by modifying the solution/feature that the DLL is a part of and redeploying the updated version.

However, an alternate solution is to write a Response Filter, which will take the output of Sharepoint and dynamically do a find/replace every single time a page is requested from Sharepoint. See http://aspnetresources.com/articles/HttpFilters for more information on how to do that.


Two parts to this, the first is to loop through all web part pages in your site - quite a few examples out there so not going to muddy things by repeating that here.

The second part is to update the Content property and save - seem like this is the missing part of your puzzle for updating the Content Editor Web Part (CEWP) programatically so :-

using System.Xml;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;

private void updateContentEditor(SPWeb web, string pageUrl)
{
    using (SPLimitedWebPartManager manager =
           web.GetLimitedWebPartManager(pageUrl, PersonalizationScope.Shared))
    {
        foreach (WebPart wp in manager.WebParts)
        {
            if (wp.GetType() == typeof(ContentEditorWebPart))
            {
                ContentEditorWebPart cewp = wp as ContentEditorWebPart;
                cewp.Content.InnerXml;

                // See http://justgeeks.blogspot.com/2009/02/i-found-to-be-bit-tricky-to-update.html
                XmlDocument xmlDoc = new XmlDocument();
                XmlElement xmlElement = xmlDoc.CreateElement("MyElement");

                // Do you change logic here
                xmlElement.InnerText =
                   contentEditor.Content.InnerText.Replace(BEFORE, AFTER);

                // Save changes
                contentEditor.Content = xmlElement;
                manager.SaveChanges(cewp);
            }
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜