SecRole.AddPrivileges Method
The AddPrivileges method adds a set of existing privileges to an existing role.
Syntax
[Visual Basic .NET]
Public Sub AddPrivileges(
ByVal Caller As CUserAuth,
ByVal RoleId As String,
ByVal Privileges As CRolePrivilege()
)
[C#]
public void AddPrivileges(
CUserAuth Caller,
string RoleId,
CRolePrivilege[] Privileges
);
[C++]
public: void AddPrivileges(
CUserAuth* Caller,
String* RoleId,
CRolePrivilege* Privileges __gc[]
);
Parameters
Caller
Specifies the identity of the caller. The caller must have the prvWriteRole privilege to perform this action. See CUserAuth.
RoleId
Specifies the ID of the role to which the privilege is being added. This ID is specified by the platform and is obtained at creation time or by a bulk retrieve.
Privileges
Specifies an array containing the IDs and depths of the privileges that are being added. Each CRolePrivilege structure should contain one string privilege ID. Privilege IDs can be obtained by calling the BizPrivilege::RetrievePrivilegeSet method. SeeCRolePrivilege.
Return Value
No return value.
Remarks
AddPrivileges fails if a privilege is added that already exists in the role, even if the depth is different. To replace a privilege, the ReplacePrivileges method should be used.
If there is an error, SOAP throws an exception and the error message is reported in System.Web.Services.Protocols.SoapException.Detail.OuterXml.
All IDs passed to the platform are GUIDs wrapped in braces. For example: {6522D89A-A752-4455-A2B0-51494C6957C3}
Example
[C#]
// strServer should be set with the name of the platform Web server
string strServer = "myservername";
// strVirtualDirectory should be set with the name of the Microsoft CRM
// virtual directory on the platform Web server
string strVirtualDirectory = "mscrmservices";
// Create the URL to the SRF files for platform objects
string strDir = "https://" + strServer + "/" + strVirtualDirectory + "/";
// BizUser proxy object
Microsoft.CRM.Proxy.BizUser user = new Microsoft.CRM.Proxy.BizUser ();
user.Credentials = System.Net.CredentialCache.DefaultCredentials;
user.Url = strDir + "BizUser.srf";
// SecRole proxy object
Microsoft.CRM.Proxy.SecRole secRole = new Microsoft.CRM.Proxy.SecRole ();
secRole.Credentials = System.Net.CredentialCache.DefaultCredentials;
secRole.Url = strDir + "SecRole.srf";
// Declare the caller
Microsoft.CRM.Proxy.CUserAuth userAuth = null;
string strSecRoleXML = "";
string strRoleId = "";
string strErrorMsg = "";
try
{
// Get the UserAuth of the current logged-on user
userAuth = user.WhoAmI();
// Set up the XML string for the role in the business unit of the caller
strSecRoleXML = "<role>" +
"<name>Custom Security Role Name</name>" +
"<businessunitid>" + userAuth.MerchantId + "</businessunitid>" +
"</role>";
// Create a custom role
// The method returns the ID of the newly created role
strRoleId = secRole.Create(userAuth, strSecRoleXML);
// Create an array of privileges to add to the desired role
Microsoft.CRM.Proxy.CRolePrivilege[] rolePrivileges = new Microsoft.CRM.Proxy.CRolePrivilege[3];
for (int i=0;i<3;i++)
{
rolePrivileges[i] = new Microsoft.CRM.Proxy.CRolePrivilege();
}
// prvCreateAccount
rolePrivileges[0].Depth = Microsoft.CRM.Proxy.PRIVILEGE_DEPTH.BASIC;
rolePrivileges[0].PrivilegeId = "{D26FE964-230B-42DD-AD93-5CC879DE411E}";
// prvReadAccount
rolePrivileges[1].Depth = Microsoft.CRM.Proxy.PRIVILEGE_DEPTH.DEEP;
rolePrivileges[1].PrivilegeId = "{886B280C-6396-4D56-A0A3-2C1B0A50CEB0}";
// prvWriteAccount
rolePrivileges[2].Depth = Microsoft.CRM.Proxy.PRIVILEGE_DEPTH.GLOBAL;
rolePrivileges[2].PrivilegeId = "{7863E80F-0AB2-4D67-A641-37D9F342C7E3}";
// Add a privilege to the role
secRole.AddPrivileges(userAuth, strRoleId, rolePrivileges);
}
catch(System.Web.Services.Protocols.SoapException err)
{
// Process the platform error here
strErrorMsg = ("ErrorMessage: " + err.Message + " " + err.Detail.OuterXml + " Source: " + err.Source );
}
catch(Exception err)
{
// Process other errors here
strErrorMsg = ("ErrorMessage: " + err.Message + "Source: " + err.Source );
}
Requirements
Namespace: Microsoft.CRM.Proxy
Assembly: microsoft.crm.proxy.dll
See Also