A post about the ReplaceLink method of a Microsoft.SharePoint.SPListItem object.
This method replaces all instances of a given absolute URL with a new absolute URL inside a SharePoint List Item.
Remarks
- Only applies to URLs formatted as hyperlink (Rich Text Field) or inside Hyperlink Fields (so this doesn't work for a plain text URL)
- Only the hyperlink URL part is replaced (even if the hyperlink text matches the URL it doesn't get replaced)
- Only works when 'Require documents to be checked out before they can be edited?' is false
- Item version, approval status and modified date remain unchanged
- Does not require item.Update(); the modification is instant and permanent
Now the biggest surprise to me was that this method also replaces URLs inside Microsoft Office Documents. The same rules (as described above) apply to documents.
Example
using (SPSite site = new SPSite("http://localhost"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.GetList("/Documents");
SPListItem item = list.Items[0];
item.ReplaceLink("http://www.sharepointblogs.com/vandest", "https://moss2007.be/blogs/vandest");
}
}
References
http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.splistitem.replacelink.aspx
Applies To
Windows SharePoint Services 3.0, Microsoft Office SharePoint Server 2007