Tuesday, April 1, 2008

To Find and Replace a String in All Pages inside Document Library in MOSS site

Hey All,

There was an interesting issue that came across in one of moss site when it went live... actually when we edit a content of a page which has content editor webpart through rich text editor, it automatically replaces the relative URL with the fully specified URL. So when the site was moved from development to production, the URLs were still pointing to the development. So to over come this issue the following aspx file with a c# code behind was written to find for a string & replace with a specfied string in All the available pages inside the document library.

Following is its code:
**********************
In-line ASPX file

System.Text
System.Data
System.Configuration
System.Collections
System.Web
System.Web.Security
System.Web.UI
System.Web.UI.WebControls
System.Web.UI.WebControls.WebParts
System.Web.UI.HtmlControls
System.Xml
Microsoft.SharePoint
Microsoft.SharePoint.Administration
Microsoft.SharePoint.Publishing
Microsoft.SharePoint.WebPartPages
System.Runtime.InteropServices
System.IO

script runat server (inside tags)

public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_LOGON_SERVICE = 3;
public const int LOGON32_PROVIDER_DEFAULT = 0;
[DllImport("advapi32.dll", CharSet=CharSet.Auto)]
public static extern bool LogonUser(
String lpszUserName,
String lpszDomain,
String lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken
);

[DllImport("kernel32.dll", CharSet=CharSet.Auto)]

public extern static bool CloseHandle(IntPtr handle);

protected void Button1_Click(object sender, EventArgs e)
{

string user = "YOUR A/C";
string userDomain = "Your Domain";
string password = "A/C Password";
bool impersonate = true;
IntPtr userHandle = new IntPtr(0);
System.Security.Principal.WindowsImpersonationContext impersonatedUser = null;
if (impersonate)
{
bool returnValue = LogonUser(
user,
userDomain,
password,
LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT,
ref userHandle
);
if (!returnValue)
{
throw new Exception("Invalid Username");
}
System.Security.Principal.WindowsIdentity newId = new System.Security.Principal.WindowsIdentity(userHandle);
impersonatedUser = newId.Impersonate();
}
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{

// We get the MOSS site URL from text box URL1.
SPSite mySite = new SPSite(URL1.Text);

SPWeb myWeb = mySite.OpenWeb();

try
{
myWeb.AllowUnsafeUpdates = true;
SPList pagesList = myWeb.Lists["Pages"];

SPListItemCollection myitems = myWeb.Lists["Pages"].Items;

foreach (SPListItem myItem in myitems)
{
SPWebPartCollection webPartCollection = myItem.File.GetWebPartCollection(Storage.Shared);

PublishingPage myPage = PublishingPage.GetPublishingPage(myItem);

foreach (Microsoft.SharePoint.WebPartPages.WebPart x in webPartCollection)
{
if (x.GetType().Name == "ContentEditorWebPart")
{
//Create an XmlElement to hold the value of the Content property.
ContentEditorWebPart ceWebPart = new ContentEditorWebPart();

//Create an XmlElement to hold the value of the Content property.
XmlDocument xmlDoc = new XmlDocument();

XmlElement xmlElement = xmlDoc.CreateElement("Content");
xmlElement.InnerText = ((ContentEditorWebPart)x).Content.InnerText.ToString();
if (xmlElement.InnerText.ToString().Contains(TextBox1.Text))
{
// We enter the text to be found in TextBox1 & the text to replace in TextBox2
xmlElement.InnerText = xmlElement.InnerText.ToString().Replace(TextBox1.Text, TextBox2.Text);

// Write the LOG details for investigation later...
StreamWriter stWriter = File.AppendText("D:\\ChangeLog.log");
stWriter.WriteLine(myItem.Url.ToString() + " - " + DateTime.Now);
stWriter.Close();

}
ceWebPart.Content = xmlElement;

((ContentEditorWebPart)x).Content = ceWebPart.Content;

webPartCollection.Web.AllowUnsafeUpdates = true;

webPartCollection.SaveChanges(x.StorageKey);

myPage.Update();

webPartCollection.Web.AllowUnsafeUpdates = false;
}
}

if (myPage.ListItem.ParentList.EnableModeration)
{
myPage.ListItem.File.Approve("");
}

}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
myWeb.Dispose();
mySite.Dispose();
}

});
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{

}

if (impersonate)
{
impersonatedUser.Undo();
CloseHandle(userHandle);
}

}

protected void Page_Load(object sender, EventArgs e)
{

}

script ending tag


Form starting tag

URL(TextBox)




From(TextBox)



To (TextBox)


Submit (Button on click calls Button1_Click event )

form ending tag

No comments: