Blog

How To Batch Update Text on a SharePoint Wiki

How To Batch Update Text on a SharePoint Wiki

SharePoint wiki pages are a great way to collaborate with coworkers and team members. You can add and edit information, links, and pictures to a wiki page through an easy process.

SharePoint wikis have a wide range of uses. Here at DMC, we use wiki pages to host our Standard Operating Procedures (SOPs). 

Our SOPs often contain links to other SharePoint pages. Recently, we reorganized our SharePoint taxonomy, otherwise known as a scheme of classification. During this process, some site's base URLs changed which caused some of the links in our wiki to become outdated.

With over 700 wiki pages, manually validating every link in each page was out of the question. To solve this problem, we developed a program using SharePoint’s client object model that replaced every outdated link with an updated one.

Client Object Model Background

The SharePoint client object model (CSOM) gives developers access to SharePoint data without requiring direct access to the server. CSOM is commonly used for updating lists and list items. In this demonstration, we will update a list of wiki pages.

Demonstration

Below is example code for updating the text of wiki pages using the client object model, which can be used for accessing SharePoint sites, lists, and items without direct access to the server. Explanations for the code are written in-line with comments.

  1. Set up a connection to the SharePoint site, and load the list to edit.
    var site = new ClientContext("http://contoso.com/");
    
    			site.Load(site.Web);
                var wikiList = site.Web.Lists.GetByTitle("Wiki List");
                site.Load(wikiList);
                site.ExecuteQuery();
    

  2. Run a query for list items. Including fields in the VIewFields tag ensures that they are returned from the Caml Query. If you would like to filter your results before they come back, add parameters to the Where section.
    CamlQuery camlQuery = new CamlQuery();
                camlQuery.ViewXml = string.Format(@"
                            
                                
                                    
                                    
                                
                            
                            
                                
                                
                            ");
    
                var items = wikiList.GetItems(camlQuery);
                site.Load(items);
                site.ExecuteQuery();
    

  3. Iterate through each wiki page and look for the text that will be replaced. If the text is found, replace it with the new text. Update the item and save the wiki page.
    foreach (ListItem item in items)
                {
                    if (item["WikiField"] != null)
                    {
                        String wikiContent = item["WikiField"].ToString();
                        if (wikiContent.Contains("href=\"http://contoso.com/sites/OldSite/"))
                        {
                            wikiContent = wikiContent.Replace(
                                "href=\"http://contoso.com/sites/OldSite/",
                                "href=\"http://contoso.com/sites/NewSite/");
    
                            item["WikiField"] = wikiContent;
                            item.Update();
                            site.ExecuteQuery();
    
                        }
                    }
                }
    

That’s it! After running this program, every wiki page in the list will have an updated URL, saving you the time and effort of manually replacing each one.

Learn More About DMC's SharePoint Consulting Services.

Comments

There are currently no comments, be the first to post one.

Post a comment

Name (required)

Email (required)

CAPTCHA image
Enter the code shown above:

Related Blog Posts