共用方式為


SPList.DoesUserHavePermissions method (SPUser, SPBasePermissions)

會指出指定的使用者是否有清單上的指定權限集。

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

Syntax

'宣告
Public Function DoesUserHavePermissions ( _
    user As SPUser, _
    permissionMask As SPBasePermissions _
) As Boolean
'用途
Dim instance As SPList
Dim user As SPUser
Dim permissionMask As SPBasePermissions
Dim returnValue As Boolean

returnValue = instance.DoesUserHavePermissions(user, _
    permissionMask)
public bool DoesUserHavePermissions(
    SPUser user,
    SPBasePermissions permissionMask
)

參數

傳回值

Type: System.Boolean
true如果使用者有權限 ;否則,請false。

Exceptions

Exception Condition
UnauthorizedAccessException

目前的使用者沒有EnumeratePermissions右邊。

Examples

下列範例會列舉中的所有使用者的網站,所有群組檢查每個使用者是否具有共享文件清單上一組特定權限的主控台應用程式。

Imports System
Imports Microsoft.SharePoint

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

            Dim perms As SPBasePermissions = SPBasePermissions.AddListItems _
                                          Or SPBasePermissions.ApproveItems _
                                          Or SPBasePermissions.CancelCheckout _
                                          Or SPBasePermissions.DeleteListItems _
                                          Or SPBasePermissions.DeleteVersions _
                                          Or SPBasePermissions.EditListItems _
                                          Or SPBasePermissions.Open _
                                          Or SPBasePermissions.OpenItems _
                                          Or SPBasePermissions.ViewListItems _
                                          Or SPBasePermissions.ViewVersions

            Dim listUrl As String = web.RootFolder.ServerRelativeUrl + "shared documents"
            Dim list As SPList = web.GetList(listUrl)

            ' Be sure the current user has permission to check permissions.
            If web.DoesUserHavePermissions(SPBasePermissions.EnumeratePermissions) Then

               For Each group As SPGroup In web.Groups
                  For Each user As SPUser In group.Users
                     ' Check another user's permissions.
                     Dim hasPermission As Boolean = list.DoesUserHavePermissions(user, perms)
                     Console.WriteLine("{0, -5}  {1}", hasPermission, user.LoginName)
                  Next
               Next

            End If

         End Using
      End Using
      Console.Write(vbCrLf + "Press ENTER to continue...")
      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.RootWeb)
            {
               SPBasePermissions perms = SPBasePermissions.AddListItems
                                       | SPBasePermissions.ApproveItems
                                       | SPBasePermissions.CancelCheckout
                                       | SPBasePermissions.DeleteListItems
                                       | SPBasePermissions.DeleteVersions
                                       | SPBasePermissions.EditListItems
                                       | SPBasePermissions.Open
                                       | SPBasePermissions.OpenItems
                                       | SPBasePermissions.ViewListItems
                                       | SPBasePermissions.ViewVersions;


               string listUrl = web.RootFolder.ServerRelativeUrl + "shared documents";
               SPList list = web.GetList(listUrl);

               // Be sure the current user has permission to check permissions.
               if (web.DoesUserHavePermissions(SPBasePermissions.EnumeratePermissions))
               {
                  foreach (SPGroup group in web.Groups)
                  {
                     foreach (SPUser user in group.Users)
                     {
                        // Check another user's permissions.
                        bool hasPermission = list.DoesUserHavePermissions(user, perms);
                        Console.WriteLine("{0, -5}  {1}", hasPermission, user.LoginName);
                     }
                  }
               }
            }
         }
         Console.Write("\nPress ENTER to continue...");
         Console.ReadLine();
      }
   }
}

請參閱

參照

SPList class

SPList members

DoesUserHavePermissions overload

Microsoft.SharePoint namespace