/// <summary> /// Validates the roles for the matter and returns the validation status. /// </summary> /// <param name="requestObject">Request Object containing SharePoint App Token</param> /// <param name="matter">Matter object containing Matter data</param> /// <param name="client">Client Object</param> /// <returns>A string value indicating whether validations passed or fail</returns> internal static string RoleCheck(RequestObject requestObject, Matter matter, Client client) { string returnValue = string.Empty; try { using (ClientContext context = ServiceUtility.GetClientContext(requestObject.SPAppToken, new Uri(ServiceConstantStrings.CentralRepositoryUrl), requestObject.RefreshToken)) { if (0 >= matter.Roles.Count()) { return(string.Format(CultureInfo.InvariantCulture, ConstantStrings.ServiceResponse, TextConstants.IncorrectInputUserRolesCode, TextConstants.IncorrectInputUserRolesMessage)); } ListItemCollection collListItem = Lists.GetData(context, ServiceConstantStrings.DMSRoleListName, ServiceConstantStrings.DMSRoleQuery); IList <string> roles = new List <string>(); roles = collListItem.AsEnumerable().Select(roleList => Convert.ToString(roleList[ServiceConstantStrings.RoleListColumnRoleName], CultureInfo.InvariantCulture)).ToList(); if (matter.Roles.Except(roles).Count() > 0) { returnValue = string.Format(CultureInfo.InvariantCulture, ConstantStrings.ServiceResponse, TextConstants.IncorrectInputUserRolesCode, TextConstants.IncorrectInputUserRolesMessage); } } } catch (Exception exception) { ProvisionHelperFunctions.DeleteMatter(requestObject, client, matter); returnValue = Logger.LogError(exception, MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ServiceConstantStrings.LogTableName); } return(returnValue); }
static void Main(string[] args) { context = new ClientContext(ConfigurationManager.AppSettings["SharepointUrl"]); GetADUsers(); string[] separatingChars = { "|" }; string[] ADGroups = ConfigurationManager.AppSettings["ADGroups"].ToString().Split(separatingChars, System.StringSplitOptions.RemoveEmptyEntries); foreach (var group in ADGroups) { using (var pcontext = new PrincipalContext(ContextType.Domain, ConfigurationManager.AppSettings["DomainAccount"], group)) { UserPrincipal userQuery = new UserPrincipal(pcontext); // get the enabled users only userQuery.Enabled = true; using (var searcher = new PrincipalSearcher()) { searcher.QueryFilter = userQuery; foreach (var result in searcher.FindAll()) { DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry; if (de.Properties["employeeNumber"].Value != null && de.Properties["company"].Value != null && de.Properties["department"].Value != null) //|| (de.Properties["distinguishedName"].Value.ToString().Contains("OU=consultants,OU=it,OU=PC") || de.Properties["distinguishedName"].Value.ToString().Contains("OU=FADA Surveyors") || de.Properties["distinguishedName"].Value.ToString().Contains("OU=CBA Surveyors")) { var item = from listitem in items.AsEnumerable() where listitem["UserLgnCode"].ToString().ToLower() == de.Properties["cn"].Value.ToString().ToLower() select listitem; if (item != null) { if (item.Count() > 0) { // update the ADGroupName in SharePoint list CAMLQuery.ViewXml = "<View><Query><Where><Eq><FieldRef Name='UserLgnCode' /><Value Type='Text'>" + de.Properties["cn"].Value.ToString().ToLower().Trim() + "</Value></Eq></Where></Query></View>"; ListItemCollection col = ADList.GetItems(CAMLQuery); context.Load(col); context.ExecuteQuery(); if (col.Count > 0) { ListItem listItem = col[0]; listItem["ADGroupName"] = CalculateADGroup(de.Properties["company"].Value.ToString().ToUpper().Trim(), de.Properties["department"].Value.ToString().ToUpper().Trim()); listItem["_DCDateModified"] = DateTime.Now; listItem.Update(); } } else { // add the user to the SharePoint list ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation(); ListItem newItem = ADList.AddItem(itemCreateInfo); newItem["UserLgnCode"] = de.Properties["cn"].Value.ToString().ToLower().Trim(); newItem["ADGroupName"] = CalculateADGroup(de.Properties["company"].Value.ToString().ToUpper().Trim(), de.Properties["department"].Value.ToString().ToUpper().Trim()); newItem["_DCDateModified"] = DateTime.Now; newItem.Update(); } } else { // add the user to the SharePoint list ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation(); ListItem newItem = ADList.AddItem(itemCreateInfo); newItem["UserLgnCode"] = de.Properties["cn"].Value.ToString().ToLower().Trim(); newItem["ADGroupName"] = CalculateADGroup(de.Properties["company"].Value.ToString().ToUpper().Trim(), de.Properties["department"].Value.ToString().ToUpper().Trim()); newItem["_DCDateModified"] = DateTime.Now; newItem.Update(); } } } } } context.ExecuteQuery(); } DeleteDisabledUsers(); }