public static void CreateSecurityGroups(IList <string> groupNames) { var context = SharePointOnlineHelper.GetElevatedContext(); var results = new Dictionary <string, int>(); try { using (context) { foreach (var group in groupNames) { try { var groupInfo = new GroupCreationInformation(); groupInfo.Title = group; context.Web.SiteGroups.Add(groupInfo); context.ExecuteQuery(); } catch (Exception e) { var c = e.Message; } } } } catch (Exception e) { var message = e.Message; } }
public static IDictionary GetSecurityGroupsFromSharePoint() { var context = SharePointOnlineHelper.GetElevatedContext(); var results = new Dictionary <string, int>(); using (context) { var groupsFromSharePoint = context.Web.SiteGroups; context.Load(groupsFromSharePoint); context.ExecuteQuery(); groupsFromSharePoint.ToList().ForEach(group => results.Add(@group.Title, @group.Id)); } return(results); }
public IHttpActionResult UpdateTalentRecord([FromBody] Talent talent) { using (var ctx = SharePointOnlineHelper.GetElevatedContext()) { LoadSecurityMatrix(talent, ctx); var list = ctx.Web.Lists.GetByTitle(ConfigurationManager.AppSettings["listName"]); var item = list.GetItemById(talent.Id); ctx.ExecuteQuery(); item = Talent.ToSPListItem(talent, item); item.Update(); item.ResetRoleInheritance(); SetPermissions(talent, item, ctx, this._roleDefinition); ctx.ExecuteQuery(); } return(Ok()); }
static void Main(string[] args) { using (var ctx = SharePointOnlineHelper.GetElevatedContext()) { CamlQuery camlQuery = new CamlQuery(); camlQuery.ViewXml = "<View><Query><Where><Contains><FieldRef Name = 'KTPDivision'/><Value Type = 'Text'>Test Data</Value></Contains></Where></Query></View>"; var list = ctx.Web.Lists.GetByTitle("Talent Records"); var itemColl = list.GetItems(camlQuery); ctx.Load(itemColl); ctx.ExecuteQuery(); var count = itemColl.Count; for (int i = 0; i < itemColl.Count; i++) { Console.WriteLine(itemColl[i]["KTPDivision"]); itemColl[i].DeleteObject(); ctx.ExecuteQuery(); } } }
public IHttpActionResult GetTalent(int id, string employeeId) { Talent talent = default(Talent); using (var ctx = SharePointOnlineHelper.GetElevatedContext()) { var list = ctx.Web.Lists.GetByTitle(ConfigurationManager.AppSettings["listName"]); var query = new CamlQuery(); query.ViewXml = KTPConstants.Get_Talent_Record_By_EmployeeId_Query.Replace("EMP_ID", employeeId); var result = list.GetItems(query); ctx.Load(result); ctx.ExecuteQuery(); talent = Talent.FromSPListItem(result[0]); if (result.ToList().Count >= 2) { talent.PreviousYear = PreviousYearRating.FromSPListItem(result[1]); } } return(Ok(talent)); }
public IHttpActionResult NewTalentRecord([FromBody] Talent talent) { try { using (var ctx = SharePointOnlineHelper.GetElevatedContext()) { LoadSecurityMatrix(talent, ctx); var list = ctx.Web.Lists.GetByTitle(ConfigurationManager.AppSettings["listName"]); ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation(); ListItem newItem = list.AddItem(itemCreateInfo); newItem = Talent.ToSPListItem(talent, newItem); newItem.Update(); SetPermissions(talent, newItem, ctx, this._roleDefinition); ctx.ExecuteQuery(); } } catch (Exception e) { return(InternalServerError(e)); } return(Ok(talent)); }
static async Task Main(string[] args) { // Load appsettings.json var config = LoadAppSettings(); if (null == config) { Console.WriteLine("Missing or invalid appsettings.json file. Please see README.md for configuration instructions."); return; } SetGlobalConfig(config); searchServiceHelper = new SearchServiceHelper(SearchServiceName, SearchServiceAdminKey); System.Diagnostics.Trace.TraceWarning("Slow response - database01"); TimeSpan elapsedTime; //Start stopwatch for timing telemtry Stopwatch sw = new Stopwatch(); var timeStart = DateTime.Now; sw.Start(); //Storage var storageAccount = CloudStorageAccount.Parse(StorageConnectionString); var storageClient = storageAccount.CreateCloudBlobClient(); AzureTableStorage azTableStorage = new AzureTableStorage(StorageConnectionString, StorageTableName); AzureTableStorage azTableStorageSpoItems = new AzureTableStorage(StorageConnectionString, SpoItemStorageTableName); CloudBlobContainer container = await AzureBLOBStorage.CreateAzureBLOBContainer(storageClient, BlobContainerName); //Search AzureSearchServiceHelper searchClient = new AzureSearchServiceHelper(SearchServiceName, SearchServiceAdminKey); IDriveItemChildrenCollectionPage docLibItems; IDriveItemDeltaCollectionPage docLibDeltaItems; for (int i = 0; i < args.Length; i++) { if (args[i].ToLower() == "-incrementalcrawl") { IncrementalCrawl = true; Console.WriteLine("Search Crawl mode set to Incremental"); container = await AzureBLOBStorage.CreateAzureBLOBContainer(storageClient, BlobContainerName); } if (args[i].ToLower() == "-fullcrawl") { IncrementalCrawl = false; Console.WriteLine("Search Crawl mode set to Full"); await AzureBLOBStorage.DeleteContainerFromAzureBLOB(container); container = await AzureBLOBStorage.CreateAzureBLOBContainer(storageClient, BlobContainerName); } if (args[i].ToLower() == "-includeacls") { IncludeAcls = true; Console.WriteLine("Search Crawl mode set to Full"); } } SharePointOnlineHelper.metadataFieldsToIgnore = MetadataFieldsToIgnore; SharePointOnlineHelper.metadataJSONStore = MetadataJSONStore; SharePointOnlineHelper.acls = IncludeAcls; SharePointOnlineHelper.azTableStorage = azTableStorageSpoItems; foreach (var metadataFieldToIgnore in MetadataFieldsToIgnore) { Console.WriteLine("Removing key [{0}] from metadata fields to extract", metadataFieldToIgnore); } //Query using Graph SDK (preferred when possible) GraphServiceClient graphClient = SharePointOnlineHelper.GetAuthenticatedGraphClient(config); Site targetSite = await graphClient.Sites.GetByPath(SiteUrl, SPOHostName).Request().GetAsync(); ISiteDrivesCollectionPage drives = graphClient.Sites[targetSite.Id].Drives.Request().GetAsync().Result; //Graph BETA supports site pages //var sitePages = graphClient.Sites[targetSite.Id].Pages.Request().GetAsync().GetAwaiter().GetResult(); //var sitePages = graphClient.Sites[targetSite.Id].Pages.Request().GetAsync().Result; //var a = 1; foreach (var drive in drives) { var driveName = drive.Name; var driveUrl = drive.WebUrl; bool excludedDocLIb = Array.Exists(DocLibsToIgnore, element => element == driveName); if (excludedDocLIb) { Console.WriteLine("Skipping [{0}] as its an excluded docLib", DocLibsToIgnore); continue; } Console.WriteLine("Fetching items from drive [{0}]", driveName); var driveId = drive.Id; var driveContents = new List <DriveItem>(); //Full Crawl Logic if (!IncrementalCrawl) { docLibItems = await graphClient .Drives[driveId] .Root .Children .Request() .GetAsync(); driveContents.AddRange(docLibItems.CurrentPage); if (docLibItems.NextPageRequest != null) { while (docLibItems.NextPageRequest != null) { docLibItems = await docLibItems.NextPageRequest.GetAsync(); driveContents.AddRange(docLibItems.CurrentPage); await SharePointOnlineHelper.GetSpoDocumentItems(graphClient, driveContents, driveId, container, IncludeAcls); } } else { await SharePointOnlineHelper.GetSpoDocumentItems(graphClient, driveContents, driveId, container, IncludeAcls); } } //Incremental Crawl Logic if (IncrementalCrawl) { //Retrieve the last known deltaToken from Table storage, if the value is null it will fetch all items for that drive //Base64 encode the string to remove special characters byte[] byt = System.Text.Encoding.UTF8.GetBytes(driveUrl); var driveUrlEscpaed = Convert.ToBase64String(byt); var lastDeltaToken = await azTableStorage.GetEntitiesInPartion(driveUrlEscpaed); docLibDeltaItems = await graphClient .Drives[driveId] .Root .Delta(lastDeltaToken) .Request() .GetAsync(); var deltaLink = docLibDeltaItems.AdditionalData["@odata.deltaLink"].ToString(); if (deltaLink != null) { var tokenindex = deltaLink.IndexOf("token="); var token = deltaLink.Substring(tokenindex + 7, deltaLink.ToString().Length - tokenindex - 9); driveContents.AddRange(docLibDeltaItems.CurrentPage); if (docLibDeltaItems.NextPageRequest != null) { while (docLibDeltaItems.NextPageRequest != null) { var docLibItems2 = await docLibDeltaItems.NextPageRequest.GetAsync(); driveContents.AddRange(docLibItems2.CurrentPage); await SharePointOnlineHelper.GetSpoDocumentItems(graphClient, driveContents, driveId, container, IncludeAcls); } } else { await SharePointOnlineHelper.GetSpoDocumentItems(graphClient, driveContents, driveId, container, IncludeAcls); //Lets persist the changeToken to storage so we can continue the next incrmental crawl from this point. IndexCrawlEntity indexCrawlEntity = new IndexCrawlEntity(driveUrlEscpaed, token); azTableStorage.InsertEntity(indexCrawlEntity); } //Console.WriteLine("Fetched total of {0} documents from [{1}] data source", DownloadFileCount, driveName); } } } if (!IncrementalCrawl) { //Now lets do a full crawl of all the fetched SPO documents from the BLOB store as the fetching of all documents into storage would have completed by now //Warning this will perform an entire search index rebuild - so while this phase is running search resultset will be impacted await IndexDocumentsAsync(); } sw.Stop(); elapsedTime = sw.Elapsed; var timeEnd = DateTime.Now; Console.WriteLine("Fetched total of {0} documents during crawl", AzureBLOBStorage.DownloadFileCount); Console.WriteLine("Crawl Start time: {0}", timeStart); Console.WriteLine("Crawl Completed time: {0}", timeEnd); Console.WriteLine("Total crawl duration time: {0}", elapsedTime); }
static void Main(string[] args) { StreamReader sr = new StreamReader(@"C:\Dev\talent-portal-refresh\Kier_Talent_DB\Provisioning\Test_Dummy_Data_2706.csv"); var csv = new CsvReader(sr); csv.Read(); //Skip Header Console.WriteLine(DateTime.Now.ToLongTimeString()); while (csv.Read()) { var employeeId = csv.GetField <string>(0); var firstName = csv.GetField <string>(2); var lastName = csv.GetField <string>(1); var fullName = string.Concat(lastName + ", " + firstName); var employeeEmail = csv.GetField <string>(3); var gender = csv.GetField <string>(4); var divison = csv.GetField <string>(5); var stream = csv.GetField <string>(6); var unit = csv.GetField <string>(7); var reportingUnit = csv.GetField <string>(8); var position = csv.GetField <string>(9); var grade = csv.GetField <string>(10); var managerNametext = csv.GetField <string>(11); var location = csv.GetField <string>(13); var performance = csv.GetField <string>(14); var potential = csv.GetField <string>(15); var flightRisk = csv.GetField <string>(16); var businessRisk = csv.GetField <string>(17); var movement = csv.GetField <string>(18); var function = csv.GetField <string>(19); var managerEmail = csv.GetField <string>(20); try { using (var ctx = SharePointOnlineHelper.GetElevatedContext()) { var talent = new Talent() { EmployeeId = employeeId, Name = new SharedKernal.Models.User() { value = employeeEmail, text = fullName }, Manager = new SharedKernal.Models.User() { value = managerEmail, text = managerNametext }, Division = divison, Stream = stream, Unit = unit, ReportingUnit = reportingUnit, ManagerName = managerNametext, Function = function, Grade = grade, Location = location, Performance = performance, Potential = potential, BusinessRisk = businessRisk, FlightRisk = flightRisk, Movement = movement, SubmissionYear = 2017, IsCurrentSubmission = false, Position = position, Gender = gender, IsLeaver = false }; var _roleDefinition = ctx.Web.RoleDefinitions.GetByType(RoleType.Reader); var _divisionAllRecordsSecurityGroup = ctx.Web.SiteGroups.GetByName(talent.GetAllDivisionRecordsGroupName()); var _streamAllRecordsSecurityGroup = ctx.Web.SiteGroups.GetByName(talent.GetAllStreamRecordsGroupName()); var _upToL1SecurityGroup = ctx.Web.SiteGroups.GetByName(talent.GetUptoL1GroupName()); var _upToL2SecurityGroup = ctx.Web.SiteGroups.GetByName(talent.GetUptoL2GroupName()); var _upToM3SecurityGroup = ctx.Web.SiteGroups.GetByName(talent.GetUpToM3GroupName()); var _talentAdmins = ctx.Web.SiteGroups.GetByName(talent.GetTalentAdminsGroupName()); ctx.Load(_divisionAllRecordsSecurityGroup); ctx.Load(_upToL1SecurityGroup); ctx.Load(_upToL2SecurityGroup); ctx.Load(_upToM3SecurityGroup); ctx.Load(_talentAdmins); ctx.ExecuteQuery(); var list = ctx.Web.Lists.GetByTitle(ConfigurationManager.AppSettings["listName"]); ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation(); ListItem newItem = list.AddItem(itemCreateInfo); newItem = Talent.ToSPListItem(talent, newItem); newItem.Update(); newItem.BreakRoleInheritance(false, true); RoleDefinitionBindingCollection collRoleDefinitionBinding = new RoleDefinitionBindingCollection(ctx); collRoleDefinitionBinding.Add(_roleDefinition); //Add access to Talent Admins and Division group newItem.RoleAssignments.Add(_talentAdmins, collRoleDefinitionBinding); newItem.RoleAssignments.Add(_divisionAllRecordsSecurityGroup, collRoleDefinitionBinding); newItem.RoleAssignments.Add(_streamAllRecordsSecurityGroup, collRoleDefinitionBinding); if (talent.IsL2Employee()) { newItem.RoleAssignments.Add(_upToL2SecurityGroup, collRoleDefinitionBinding); } if (talent.IsL1Employee()) { newItem.RoleAssignments.Add(_upToL2SecurityGroup, collRoleDefinitionBinding); newItem.RoleAssignments.Add(_upToL1SecurityGroup, collRoleDefinitionBinding); } if (talent.IsCorMEmployee()) { newItem.RoleAssignments.Add(_upToL2SecurityGroup, collRoleDefinitionBinding); newItem.RoleAssignments.Add(_upToL1SecurityGroup, collRoleDefinitionBinding); newItem.RoleAssignments.Add(_upToM3SecurityGroup, collRoleDefinitionBinding); } ctx.ExecuteQuery(); } } catch (Exception e) { Console.WriteLine(e.Message); } } //csv.Configuration.HeaderValidated = null; //csv.Configuration.MissingFieldFound = null; //var records = csv.GetRecords<Talent>().ToList(); Console.WriteLine(DateTime.Now.ToLongTimeString()); Console.Read(); }