Sunday, February 21, 2016

Hide Sharepoint folders for network location WebDAV interface

As you probably know Sharepoint supports WebDAV protocol. It allows you to connect to Sharepoint site via regular Windows Explorer: open My Computer > right click on the area under all drives > Choose Add a network location in context menu:

In the opened window you have to specify address of the site and Exporer will show all document libraries, sub sites and folders created for specified site. This functuionality works via WebDAV protocol. Sometime we don’t want to show all these folders to end users. In this case we may hide the folder like we do it for regular Windows folders on the local file system:

Important to note that like in case with regular Windows folder when we hide Sharepoint folder via WebDAV it affects all users which open the same site via network location (however hiding folders there doesn’t affect web interface view, i.e. they still will be shown in the browser). When you hide folder from the folder property the following PROPPATCH HTTP request is sent to the server:

PROPPATCH http://example.com/test/test HTTP/1.1
Cache-Control: no-cache
Pragma: no-cache
Content-Type: text/xml; charset="utf-8"
User-Agent: Microsoft-WebDAV-MiniRedir/6.1.7601
translate: f
Connection: Keep-Alive
Content-Length: 215
Host: example.com
<?xml version="1.0" encoding="utf-8" ?><D:propertyupdate xmlns:D="DAV:" xmlns:Z="urn:schemas-microsoft-com:"><D:set><D:prop><Z:Win32FileAttributes>00000010</Z:Win32FileAttributes></D:prop></D:set></D:propertyupdate>

If we will check folder’s properties using the following console utility:

   1: private static void CheckFolderProperties(SPFolder f)
   2: {
   3:     Console.WriteLine("Check properties of '{0}' folder", f.Name);
   4:     foreach (DictionaryEntry p in f.Properties)
   5:     {
   6:         Console.WriteLine("\t{0}: '{1}'", p.Key, p.Value);
   7:     }
   8: }

We will find that when we set Hidden attribute for the folder vti_winfileattribs property is changed from 00000010 to 00000012 value. With this information it is quite simple to hide folder for network location interface programmatically. The only thing which we need to do is to set vti_winfileattribs property for the folder:

   1: SPFolder f = ...;
   2: f.Properties["vti_winfileattribs"] = "00000012";
   3: f.Update();

After that it will be hidden when site is displayed via network location.