2015-04-23 55 views
-2

我想通過使用Sitecore規則將用戶添加到特定角色。我正在考慮在/ Actions/New User Action項目中的項目已保存事件中編寫代碼,但我不確定要編寫什麼。是否有可能通過規則將用戶添加到角色?如果是這樣,我怎樣才能使用類型,代碼,引用和語言字段?如何使用sitecore規則中的規則和操作

+0

請嘗試文檔:http://sdn.sitecore.net/upload/sitecore7/70/rules_engine_cookbook_sc70-a4.pdf –

+0

我已經通過這個文檔。我沒有找到如何編寫代碼字段中的C#代碼。我是試圖編寫代碼在「/ sitecore /系統/設置/規則/項目保存/操作/示例」項目路徑 –

回答

0

與下面的代碼創建組裝,要麼調用組件的[「事件名稱=」項:保存「。在類型字段處理程序app_include或將‘Namespace.AddNewUser.AddUser,yourAssembly’

public void AddUser(string domain, string firstName, string lastName, string email, 
     string comment, string telephoneNumber, string jobTitle) 
{ 
    string userName = string.Concat(firstName, lastName); 
    userName = string.Format(@"{0}\{1}", domain, userName); 

    // You can manually set the password or from Sitecore's generator 
    string newPassword = Membership.GeneratePassword(10, 3); 

    try 
    { 
     if (!User.Exists(userName)) 
     { 
      Membership.CreateUser(userName, newPassword, email); 

      // Edit the profile information 
      Sitecore.Security.Accounts.User user = Sitecore.Security.Accounts.User.FromName(userName, true); 
      Sitecore.Security.UserProfile userProfile = user.Profile; 
      userProfile.FullName = string.Format("{0} {1}", firstName, lastName); 
      userProfile.Comment = comment; 

      // Assigning the user profile template 
      userProfile.SetPropertyValue("ProfileItemId", "{this is profileItem's GUID}"); 

      // Have modified the user template to also contain telephone number and job title. 
      userProfile.SetCustomProperty("TelephoneNumber", telephoneNumber); 
      userProfile.SetCustomProperty("JobTitle", jobTitle); 
      userProfile.Save(); 
     } 
    } 
    catch (Exception ex) 
    { 
     Sitecore.Diagnostics.Log.Error(string.Format("Error in Client.Project.Security.UserMaintenance (AddUser): Message: {0}; Source:{1}", ex.Message, ex.Source), this); 
    } 
} 
+0

感謝reply.how該規則適用於此腳本 –

+0

我認爲你已經在sitecore中定義規則,所以添加規則條件在執行此方法之前。 – Jay