SPWeb.GetFolder method (Guid)

Retrieves the folder object with the specified GUID.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'Declaration
Public Function GetFolder ( _
    uniqueId As Guid _
) As SPFolder
'Usage
Dim instance As SPWeb
Dim uniqueId As Guid
Dim returnValue As SPFolder

returnValue = instance.GetFolder(uniqueId)
public SPFolder GetFolder(
    Guid uniqueId
)

Parameters

  • uniqueId
    Type: System.Guid

    A GUID that identifies the folder.

Return value

Type: Microsoft.SharePoint.SPFolder
The folder with the specified GUID.

Remarks

The user must have BrowseDirectories permissions to use this method.

Examples

The following example is a console application that demonstrates two ways of getting an SPFolder object for a folder named "Test folder" in the "Shared Documents" list.

Imports System
Imports Microsoft.SharePoint

Module ConsoleApp
   Sub Main()
      Using site As SPSite = New SPSite("https://localhost")
         Using web As SPWeb = site.OpenWeb()

                If (web.DoesUserHavePermissions(SPBasePermissions.BrowseDirectories)) Then

                    ' Get a folder by server-relative URL.
                    Dim url As String = web.ServerRelativeUrl + "/shared documents/test folder"
                    Dim folder As SPFolder = web.GetFolder(url)

                    Try
                        ' Get the folder's Guid.
                        Dim id As Guid = folder.UniqueId
                        Console.WriteLine(id)

                        ' Get a folder by Guid.
                        folder = web.GetFolder(id)
                        url = folder.ServerRelativeUrl
                        Console.WriteLine(url)

                    Catch ex As System.IO.FileNotFoundException
                        Console.WriteLine(ex.Message)
                    End Try

                End If

         End Using
      End Using
      Console.ReadLine()
   End Sub
End Module
using System;
using Microsoft.SharePoint;

namespace Test
{
   class ConsoleApp
   {
      static void Main(string[] args)
      {
         using (SPSite site = new SPSite("https://localhost"))
         {
            using (SPWeb web = site.OpenWeb())
            {
                    if (web.DoesUserHavePermissions(SPBasePermissions.BrowseDirectories))
                    {
                        // Get a folder by server-relative URL.
                        string url = web.ServerRelativeUrl + "/shared documents/test folder";
                        SPFolder folder = web.GetFolder(url);

                        try
                        {
                            // Get the folder's Guid.
                            Guid id = folder.UniqueId;
                            Console.WriteLine(id);

                            // Get a folder by Guid.
                            folder = web.GetFolder(id);
                            url = folder.ServerRelativeUrl;
                            Console.WriteLine(url);
                        }
                        catch (System.IO.FileNotFoundException ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
            }
         }
         Console.ReadLine();
      }
   }
}

See also

Reference

SPWeb class

SPWeb members

GetFolder overload

Microsoft.SharePoint namespace