SharePoint Dragons

Nikander & Margriet on SharePoint

Retrieve folder using client object model that has space in its name

You have to be a little bit careful when you’re retrieving a SharePoint folder name using the client object model that has a space in its name. The correct way to do it is to replace the space with “%20”:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Client;

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            try

            {

                //string folderName = “/MyDox/Test”;

                string folderName = “/MyDox/Name%20withSpace”;

                ClientContext context = new ClientContext(“http://astro“);

                Web web = context.Web;

                List list = context.Web.Lists.GetByTitle(“MyDox”);

Folder folder = web.GetFolderByServerRelativeUrl(folderName);               

context.Load(context.Web);

                context.Load(list);

                context.Load(folder);               

                context.ExecuteQuery();

                Console.WriteLine(folder.Name);

            }

            catch (Exception err)

            {

                Console.WriteLine(err.Message);

            }

        }

    }

}

2 responses to “Retrieve folder using client object model that has space in its name

  1. Bjoern H Rapp June 20, 2012 at 9:38 pm

    Hi Margriet. Nice sample, although I’d guess this line: List list = context.Web.Lists.GetByTitle(“MyDox”); and the corresponding Load() are leftovers.

Leave a reply to Bjoern H Rapp Cancel reply