Create Wiki Pages In SharePoint using List Web Service

This is a strange one as it caused some unexpected results during the development process – I needed to create a whole load of wiki pages in numerious wiki libraries (over 200 pages as it happens) both locally on my development box and then on the production box.   So I looked at Web Services from SharePoint 2010 and found that in the List web service there was a couple of methods I could actually use:-

AddWikiPage

AddWikiPageAsync

Essentially my code would loop around and call the method of my choice from above to create the wiki pages, in each of the methods there are 3 arguments:-

Argument 1: {name of wiki Library}

Argument 2: {name of wiki page to create}

Argument 3: {content of the wiki field}

All three arguments are strings, and the third argument for the method(s) can be the HTML content to display.  Anyway to cut a long strory short I tried the first method AddWikiPage – and all I got was a lot of pain, as my console application would bomb out.  So I then went and tried the second method AddWikiPageAsync – which by all accounts would create the pages Asynchrously in the processors own time.  This seemed to work but it was very random in that it would create may 1 page or 3 pages but never the full amount.  So after almost tearing my head out one of my colleagues said why don’t you put a little pause in between the calls to the method.  At first I thought it was a silly idea – but I had nothing to lose and used the Sleep method from the System.Threading .Net library to pause between each call to AddWikiPageAsync for one second in each page creation.  Would you believe it – it actually worked !!!

The following is the final code method:-

public static void CreateWikiPages(List<string> Pages, string LibName)
{
            SPReference.Lists client = new SPReference.Lists();           
            client.Credentials = new NetworkCredential();
            client.Credentials = CredentialCache.DefaultCredentials;
            try
            {
                int index = 0;
                if (Pages.Count > 0)
                {
                    while (index < Pages.Count)
                    {
                        string pageName = Pages[index] + “.aspx”;                                 
                        client.AddWikiPageAsync(LibName, pageName, “<h3>” + Pages[index] + “</h3>”);                       
                        Thread.Sleep(1000);                   
                        Console.WriteLine(“Page Created: ” + pageName );                     
                        index++;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(“Page Error \n” + e.Message);
                if (e.InnerException != null)
                    Console.WriteLine(“Page Error \n” + e.InnerException.Message);
            }
}

Notes for the above code:-

client – this is the reference to my web service to the SharePoint List web service.

Pages – this is just an array that contains a load of strings which are the names of my pages that I will create.

LibName – the name of the wiki library in which to create my pages

:

5 Responses to “Create Wiki Pages In SharePoint using List Web Service”

  1. disposable says:

    It’s really a nice and useful piece of info. I am satisfied that you shared this helpful information with us. Please keep us up to date like this. Thanks for sharing.

  2. mobile says:

    Thank you for another magnificent post. The place else may anyone get that type of information in such an ideal approach of writing? I have a presentation next week, and I’m at the look for such information.

    • Johnny says:

      Thanks for this, I’ll try to use this as I need to create 1300 Wiki pages. :)
      Is there any way to put some “default” text into the pages and also something about the text layout (e.g 2 columns with header). Is there any variable that that I can use ? – if you know of course.

      Thank you very much in advance,

      johnny.

      • ISUDDIN says:

        Hi Johnny

        If I remember correctly there is a column that exists for Wiki Pages to contain the actual content – I think you will be able to inject some HTML into that field. Wiki Pages themselves are just list items within a Wiki Library. I guess a list item update into that column should do the trick.

        Regards,

        MyTechBook Admin

Leave a Reply