Back to Devexpress

SubDocument.BeginUpdateRangePermissions() Method

officefileapi-devexpress-dot-xtrarichedit-dot-api-dot-native-dot-subdocument-6c4c5d12.md

latest9.0 KB
Original Source

SubDocument.BeginUpdateRangePermissions() Method

Gets the collection of all range permissions in the current document.

Namespace : DevExpress.XtraRichEdit.API.Native

Assembly : DevExpress.RichEdit.v25.2.Core.dll

NuGet Package : DevExpress.RichEdit.Core

Declaration

csharp
RangePermissionCollection BeginUpdateRangePermissions()
vb
Function BeginUpdateRangePermissions As RangePermissionCollection

Returns

TypeDescription
RangePermissionCollection

A RangePermissionCollection containing range permissions for the document.

|

Remarks

This code snippet demonstrates how you can fetch usernames assigned to protected ranges in a document and identify the current user as being one of the particular users. It allows you to edit the range with editing permissions granted to that user.

The SubDocument.BeginUpdateRangePermissions method is used to get all permissions for document ranges. Call the SubDocument.CancelUpdateRangePermissions if you do not intend to modify them.

Then, we traverse the collection of range permissions and obtain user names via the RangePermission.UserName property. The ComboBoxEdit control is filled with the names, so an existing name can be selected to log in.

To log in (i.e. to specify the user whose permissions will be in effect), the RichEditControlOptionsBase.Authentication options are used.

csharp
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using DevExpress.XtraRichEdit.Services;
        void richEditControl1_DocumentLoaded(object sender, EventArgs e)
        {
            UpdateUserNameLoginCombo();
            richEditControl1.Options.Authentication.UserName = String.Empty;
        }
        private void UpdateUserNameLoginCombo()
        {
            cmbUserName.SelectedValueChanged-=cmbUserName_SelectedValueChanged;
            RangePermissionCollection rangePermissions = richEditControl1.Document.BeginUpdateRangePermissions();
            richEditControl1.Document.CancelUpdateRangePermissions(rangePermissions);
            List<String> users = new List<string>();
            foreach (RangePermission rangePermission in rangePermissions)
            {
                string userName = rangePermission.UserName;
                if (users.Contains(userName))
                    continue;
                if (!String.IsNullOrEmpty(userName))
                    users.Add(userName);
            }
            cmbUserName.Properties.BeginUpdate();
            cmbUserName.Properties.Items.Clear();
            cmbUserName.Properties.Items.Add(String.Empty);
            cmbUserName.Properties.Items.AddRange(users);
            foreach (MyUser user in myUserList) {
                if (!users.Contains(user.UserName)) cmbUserName.Properties.Items.Add(user.UserName);
            }
            cmbUserName.SelectedIndex = 0;
            cmbUserName.Properties.EndUpdate();
            cmbUserName.SelectedValueChanged += cmbUserName_SelectedValueChanged;
        }

        private void cmbUserName_SelectedValueChanged(object sender, EventArgs e)
        {
            string username = cmbUserName.SelectedItem.ToString();
            richEditControl1.Options.Authentication.UserName = username;
            MyUser myuser = myUserList.Find(s => s.UserName == username);
            richEditControl1.Options.Authentication.Group = (myuser != null) ? myuser.Group : String.Empty;

        }
vb
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native
Imports DevExpress.XtraRichEdit.Services
        Private Sub richEditControl1_DocumentLoaded(ByVal sender As Object, ByVal e As EventArgs)
            UpdateUserNameLoginCombo()
            richEditControl1.Options.Authentication.UserName = String.Empty
        End Sub
        Private Sub UpdateUserNameLoginCombo()
            RemoveHandler cmbUserName.SelectedValueChanged, AddressOf cmbUserName_SelectedValueChanged
            Dim rangePermissions As RangePermissionCollection = richEditControl1.Document.BeginUpdateRangePermissions()
            richEditControl1.Document.CancelUpdateRangePermissions(rangePermissions)
            Dim users As New List(Of String)()
            For Each rangePermission As RangePermission In rangePermissions
                Dim userName As String = rangePermission.UserName
                If users.Contains(userName) Then
                    Continue For
                End If
                If Not String.IsNullOrEmpty(userName) Then
                    users.Add(userName)
                End If
            Next rangePermission
            cmbUserName.Properties.BeginUpdate()
            cmbUserName.Properties.Items.Clear()
            cmbUserName.Properties.Items.Add(String.Empty)
            cmbUserName.Properties.Items.AddRange(users)
            For Each user As MyUser In myUserList
                If Not users.Contains(user.UserName) Then
                    cmbUserName.Properties.Items.Add(user.UserName)
                End If
            Next user
            cmbUserName.SelectedIndex = 0
            cmbUserName.Properties.EndUpdate()
            AddHandler cmbUserName.SelectedValueChanged, AddressOf cmbUserName_SelectedValueChanged
        End Sub

        Private Sub cmbUserName_SelectedValueChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cmbUserName.SelectedValueChanged
            Dim username As String = cmbUserName.SelectedItem.ToString()
            richEditControl1.Options.Authentication.UserName = username
            Dim myuser As MyUser = myUserList.Find(Function(s) s.UserName = username)
            richEditControl1.Options.Authentication.Group = If(myuser IsNot Nothing, myuser.Group, String.Empty)

        End Sub

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the BeginUpdateRangePermissions() method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

word-document-api-examples/CS/CodeExamples/ProtectionActions.cs#L96

csharp
// Access the range permissions collection.
RangePermissionCollection rangePermissions = document.BeginUpdateRangePermissions();

word-document-api-examples/VB/CodeExamples/ProtectionActions.vb#L78

vb
' Access the range permissions collection.
Dim rangePermissions As DevExpress.XtraRichEdit.API.Native.RangePermissionCollection = document.BeginUpdateRangePermissions()
If document.Paragraphs.Count > 3 Then

See Also

CancelUpdateRangePermissions(RangePermissionCollection)

EndUpdateRangePermissions(RangePermissionCollection)

Authentication

IsDocumentProtected

ProtectDocumentCommand

UnprotectDocumentCommand

SubDocument Interface

SubDocument Members

DevExpress.XtraRichEdit.API.Native Namespace