public static int AddSite(SharePointSite item) { // check account int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive); if (accountCheck < 0) { return(accountCheck); } // check package int packageCheck = SecurityContext.CheckPackage(item.PackageId, DemandPackage.IsActive); if (packageCheck < 0) { return(packageCheck); } // check quota QuotaValueInfo quota = PackageController.GetPackageQuota(item.PackageId, Quotas.SHAREPOINT_SITES); if (quota.QuotaExhausted) { return(BusinessErrorCodes.ERROR_SHAREPOINT_RESOURCE_QUOTA_LIMIT); } // check if stats resource is available int serviceId = PackageController.GetPackageServiceId(item.PackageId, ResourceGroups.SharePoint); if (serviceId == 0) { return(BusinessErrorCodes.ERROR_SHAREPOINT_RESOURCE_UNAVAILABLE); } // check package items if (PackageController.GetPackageItemByName(item.PackageId, item.Name, typeof(SharePointSite)) != null) { return(BusinessErrorCodes.ERROR_SHAREPOINT_PACKAGE_ITEM_EXISTS); } // place log record TaskManager.StartTask("SHAREPOINT", "ADD_SITE", item.Name); TaskManager.WriteParameter("Database group", item.DatabaseGroupName); TaskManager.WriteParameter("Database name", item.DatabaseName); TaskManager.WriteParameter("Database user", item.DatabaseUser); int databaseItemId = 0; int databaseUserItemId = 0; try { // load web site WebSite siteItem = (WebSite)PackageController.GetPackageItemByName(item.PackageId, item.Name, typeof(WebSite)); if (siteItem == null) { return(BusinessErrorCodes.ERROR_WEB_SITE_SERVICE_UNAVAILABLE); } // get service web site WebServer web = new WebServer(); ServiceProviderProxy.Init(web, siteItem.ServiceId); WebSite site = web.GetSite(siteItem.SiteId); ///////////////////////////////////////// // // PREPARE SHAREPOINT SITE INSTALLATION // ServiceInfo wssService = ServerController.GetServiceInfo(serviceId); bool wss30 = (wssService.ProviderId == 23); // WSS 3.0 // remember original web site bindings ServerBinding[] bindings = site.Bindings; // set application pool and root folder item.ApplicationPool = site.ApplicationPool; item.RootFolder = site.ContentPath; // change web site .NET framework if required bool siteUpdated = false; if (!wss30 && (site.AspNetInstalled != "1" || site.DedicatedApplicationPool)) { site.AspNetInstalled = "1"; site.DedicatedApplicationPool = false; web.UpdateSite(site); siteUpdated = true; } if (wss30 && site.AspNetInstalled != "2") { site.AspNetInstalled = "2"; web.UpdateSite(site); siteUpdated = true; } if (siteUpdated) { site = web.GetSite(siteItem.SiteId); item.ApplicationPool = site.ApplicationPool; } if (site.FrontPageInstalled) { // remove FrontPage web.UninstallFrontPage(siteItem.SiteId, siteItem.FrontPageAccount); } // create SQL database SqlDatabase database = new SqlDatabase(); database.PackageId = item.PackageId; database.Name = item.DatabaseName; databaseItemId = DatabaseServerController.AddSqlDatabase(database, item.DatabaseGroupName); if (databaseItemId < 0) { return(databaseItemId); } // create SQL user SqlUser dbUser = new SqlUser(); dbUser.PackageId = item.PackageId; dbUser.Name = item.DatabaseUser; dbUser.Password = item.DatabasePassword; databaseUserItemId = DatabaseServerController.AddSqlUser(dbUser, item.DatabaseGroupName); if (databaseUserItemId < 0) { return(databaseUserItemId); } // delete SQL database from service // and change database user role int sqlServiceId = PackageController.GetPackageServiceId(item.PackageId, item.DatabaseGroupName); if (sqlServiceId < 0) { return(BusinessErrorCodes.ERROR_MSSQL_RESOURCE_UNAVAILABLE); } // load server settings StringDictionary sqlSettings = ServerController.GetServiceSettings(sqlServiceId); item.DatabaseServer = sqlSettings["ExternalAddress"]; DatabaseServer dbServer = new DatabaseServer(); ServiceProviderProxy.Init(dbServer, sqlServiceId); // delete database from service dbServer.DeleteDatabase(database.Name); // give SQL user "db_creator" role dbServer.ExecuteSqlNonQuery("master", String.Format( "sp_addsrvrolemember '{0}', 'dbcreator'\nGO", dbUser.Name)); // install SharePoint site SharePointServer sps = GetSharePoint(serviceId); sps.ExtendVirtualServer(item); // remove SQL user from "db_creator" role dbServer.ExecuteSqlNonQuery("master", String.Format( "sp_dropsrvrolemember '{0}', 'dbcreator'\nGO", dbUser.Name)); // restore original web site bindings web.UpdateSiteBindings(site.SiteId, bindings, false); // save statistics item item.ServiceId = serviceId; int itemId = PackageController.AddPackageItem(item); TaskManager.ItemId = itemId; return(itemId); } catch (Exception ex) { // delete database if required if (databaseItemId > 0) { DatabaseServerController.DeleteSqlDatabase(databaseItemId); } // delete user if required if (databaseUserItemId > 0) { DatabaseServerController.DeleteSqlUser(databaseUserItemId); } throw TaskManager.WriteError(ex); } finally { TaskManager.CompleteTask(); } }
public static string BackupVirtualServer(int itemId, string fileName, bool zipBackup, bool download, string folderName) { // check account int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo); if (accountCheck < 0) { return(null); } // load original meta item SharePointSite item = (SharePointSite)PackageController.GetPackageItem(itemId); if (item == null) { return(null); } // place log record TaskManager.StartTask("SHAREPOINT", "BACKUP_SITE", item.Name, itemId); try { SharePointServer sps = GetSharePoint(item.ServiceId); string backFile = sps.BackupVirtualServer(item.Name, fileName, zipBackup); if (!download) { // copy backup files to space folder string relFolderName = FilesController.CorrectRelativePath(folderName); if (!relFolderName.EndsWith("\\")) { relFolderName = relFolderName + "\\"; } // create backup folder if not exists if (!FilesController.DirectoryExists(item.PackageId, relFolderName)) { FilesController.CreateFolder(item.PackageId, relFolderName); } string packageFile = relFolderName + Path.GetFileName(backFile); // delete destination file if exists if (FilesController.FileExists(item.PackageId, packageFile)) { FilesController.DeleteFiles(item.PackageId, new string[] { packageFile }); } byte[] buffer = null; int offset = 0; do { // read remote content buffer = sps.GetTempFileBinaryChunk(backFile, offset, FILE_BUFFER_LENGTH); // write remote content FilesController.AppendFileBinaryChunk(item.PackageId, packageFile, buffer); offset += FILE_BUFFER_LENGTH; }while (buffer.Length == FILE_BUFFER_LENGTH); } return(backFile); } catch (Exception ex) { throw TaskManager.WriteError(ex); } finally { TaskManager.CompleteTask(); } }
public static int InstallWebPartsPackage(int itemId, string uploadedFile, string packageFile) { // check account int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive); if (accountCheck < 0) { return(accountCheck); } // load original meta item SharePointSite item = (SharePointSite)PackageController.GetPackageItem(itemId); if (item == null) { return(BusinessErrorCodes.ERROR_SHAREPOINT_PACKAGE_ITEM_NOT_FOUND); } // check package int packageCheck = SecurityContext.CheckPackage(item.PackageId, DemandPackage.IsActive); if (packageCheck < 0) { return(packageCheck); } // place log record TaskManager.StartTask("SHAREPOINT", "INSTALL_WEBPARTS", item.Name, itemId); TaskManager.WriteParameter("Package file", packageFile); try { SharePointServer sps = GetSharePoint(item.ServiceId); string backupFile = null; if (!String.IsNullOrEmpty(packageFile)) { // copy package files to the remote SharePoint Server string path = null; byte[] buffer = null; int offset = 0; do { // read package file buffer = FilesController.GetFileBinaryChunk(item.PackageId, packageFile, offset, FILE_BUFFER_LENGTH); // write remote backup file string tempPath = sps.AppendTempFileBinaryChunk(Path.GetFileName(packageFile), path, buffer); if (path == null) { path = tempPath; backupFile = path; } offset += FILE_BUFFER_LENGTH; }while (buffer.Length == FILE_BUFFER_LENGTH); } else if (!String.IsNullOrEmpty(uploadedFile)) { // upladed files backupFile = uploadedFile; } // restore if (!String.IsNullOrEmpty(backupFile)) { sps.InstallWebPartsPackage(item.Name, backupFile); } return(0); } catch (Exception ex) { throw TaskManager.WriteError(ex); } finally { TaskManager.CompleteTask(); } }
public static async Task RunAsync([QueueTrigger("actions")] ProvisioningActionModel action, TextWriter log) { var startProvisioning = DateTime.Now; String provisioningEnvironment = ConfigurationManager.AppSettings["SPPA:ProvisioningEnvironment"]; log.WriteLine($"Processing queue trigger function for {action.UserPrincipalName} on tenant {action.TenantId}"); log.WriteLine($"PnP Correlation ID: {action.CorrelationId.ToString()}"); // Instantiate and use the telemetry model TelemetryUtility telemetry = new TelemetryUtility(log); Dictionary <string, string> telemetryProperties = new Dictionary <string, string>(); // Configure telemetry properties // telemetryProperties.Add("UserPrincipalName", action.UserPrincipalName); // telemetryProperties.Add("TenantId", action.TenantId); telemetryProperties.Add("PnPCorrelationId", action.CorrelationId.ToString()); telemetryProperties.Add("TargetSiteAlreadyExists", action.TargetSiteAlreadyExists.ToString()); telemetryProperties.Add("TargetSiteBaseTemplateId", action.TargetSiteBaseTemplateId); var appOnlyAccessToken = await ProvisioningAppManager.AccessTokenProvider.GetAppOnlyAccessTokenAsync( "https://graph.microsoft.com/", ConfigurationManager.AppSettings["OfficeDevPnP:TenantId"], ConfigurationManager.AppSettings["OfficeDevPnP:ClientId"], ConfigurationManager.AppSettings["OfficeDevPnP:ClientSecret"], ConfigurationManager.AppSettings["OfficeDevPnP:AppUrl"]); // Get a reference to the data context ProvisioningAppDBContext dbContext = new ProvisioningAppDBContext(); try { // Log telemetry event telemetry?.LogEvent("ProvisioningFunction.Start"); var tokenId = $"{action.TenantId}-{action.UserPrincipalName.GetHashCode()}-{action.ActionType.ToString().ToLower()}-{provisioningEnvironment}"; // Retrieve the SPO target tenant via Microsoft Graph var graphAccessToken = await ProvisioningAppManager.AccessTokenProvider.GetAccessTokenAsync( tokenId, "https://graph.microsoft.com/", ConfigurationManager.AppSettings[$"{action.ActionType}:ClientId"], ConfigurationManager.AppSettings[$"{action.ActionType}:ClientSecret"], ConfigurationManager.AppSettings[$"{action.ActionType}:AppUrl"]); if (!String.IsNullOrEmpty(graphAccessToken)) { #region Get current context data (User, SPO Tenant, SPO Access Token) // Get the currently connected user name and email (UPN) var jwtAccessToken = new System.IdentityModel.Tokens.Jwt.JwtSecurityToken(graphAccessToken); String delegatedUPN = String.Empty; var upnClaim = jwtAccessToken.Claims.FirstOrDefault(c => c.Type == "upn"); if (upnClaim != null && !String.IsNullOrEmpty(upnClaim.Value)) { delegatedUPN = upnClaim.Value; } String delegatedUserName = String.Empty; var nameClaim = jwtAccessToken.Claims.FirstOrDefault(c => c.Type == "name"); if (nameClaim != null && !String.IsNullOrEmpty(nameClaim.Value)) { delegatedUserName = nameClaim.Value; } // Determine the URL of the root SPO site for the current tenant var rootSiteJson = HttpHelper.MakeGetRequestForString("https://graph.microsoft.com/v1.0/sites/root", graphAccessToken); SharePointSite rootSite = JsonConvert.DeserializeObject <SharePointSite>(rootSiteJson); String spoTenant = rootSite.WebUrl; log.WriteLine($"Target SharePoint Online Tenant: {spoTenant}"); // Configure telemetry properties telemetryProperties.Add("SPOTenant", spoTenant); // Retrieve the SPO Access Token var spoAccessToken = await ProvisioningAppManager.AccessTokenProvider.GetAccessTokenAsync( tokenId, rootSite.WebUrl, ConfigurationManager.AppSettings[$"{action.ActionType}:ClientId"], ConfigurationManager.AppSettings[$"{action.ActionType}:ClientSecret"], ConfigurationManager.AppSettings[$"{action.ActionType}:AppUrl"]); log.WriteLine($"Retrieved target SharePoint Online Access Token."); #endregion // Connect to SPO, create and provision site AuthenticationManager authManager = new AuthenticationManager(); using (ClientContext context = authManager.GetAzureADAccessTokenAuthenticatedContext(spoTenant, spoAccessToken)) { // Telemetry and startup var web = context.Web; context.ClientTag = $"SPDev:ProvisioningPortal-{provisioningEnvironment}"; context.Load(web, w => w.Title, w => w.Id); await context.ExecuteQueryAsync(); log.WriteLine($"SharePoint Online Root Site Collection title: {web.Title}"); #region Store the main site URL in KeyVault // Store the main site URL in KeyVault var vault = new KeyVaultService(); // Read any existing properties for the current tenantId var properties = await vault.GetAsync(tokenId); if (properties == null) { // If there are no properties, create a new dictionary properties = new Dictionary <String, String>(); } // Set/Update the RefreshToken value properties["SPORootSite"] = spoTenant; // Add or Update the Key Vault accordingly await vault.AddOrUpdateAsync(tokenId, properties); #endregion #region Provision the package var package = dbContext.Packages.FirstOrDefault(p => p.Id == new Guid(action.PackageId)); if (package != null) { // Update the Popularity of the package package.TimesApplied++; dbContext.SaveChanges(); #region Get the Provisioning Hierarchy file // Determine reference path variables var blobConnectionString = ConfigurationManager.AppSettings["BlobTemplatesProvider:ConnectionString"]; var blobContainerName = ConfigurationManager.AppSettings["BlobTemplatesProvider:ContainerName"]; var packageFileName = package.PackageUrl.Substring(package.PackageUrl.LastIndexOf('/') + 1); var packageFileUri = new Uri(package.PackageUrl); var packageFileRelativePath = packageFileUri.AbsolutePath.Substring(2 + blobContainerName.Length); var packageFileRelativeFolder = packageFileRelativePath.Substring(0, packageFileRelativePath.LastIndexOf('/')); // Configure telemetry properties telemetryProperties.Add("PackageFileName", packageFileName); telemetryProperties.Add("PackageFileUri", packageFileUri.ToString()); // Read the main provisioning file from the Blob Storage CloudStorageAccount csa; if (!CloudStorageAccount.TryParse(blobConnectionString, out csa)) { throw new ArgumentException("Cannot create cloud storage account from given connection string."); } CloudBlobClient blobClient = csa.CreateCloudBlobClient(); CloudBlobContainer blobContainer = blobClient.GetContainerReference(blobContainerName); var blockBlob = blobContainer.GetBlockBlobReference(packageFileRelativePath); // Crate an in-memory copy of the source stream MemoryStream mem = new MemoryStream(); await blockBlob.DownloadToStreamAsync(mem); mem.Position = 0; // Prepare the output hierarchy ProvisioningHierarchy hierarchy = null; if (packageFileName.EndsWith(".xml", StringComparison.InvariantCultureIgnoreCase)) { // That's an XML Provisioning Template file XDocument xml = XDocument.Load(mem); mem.Position = 0; // Deserialize the stream into a provisioning hierarchy reading any // dependecy with the Azure Blob Storage connector var formatter = XMLPnPSchemaFormatter.GetSpecificFormatter(xml.Root.Name.NamespaceName); var templateLocalFolder = $"{blobContainerName}/{packageFileRelativeFolder}"; var provider = new XMLAzureStorageTemplateProvider( blobConnectionString, templateLocalFolder); formatter.Initialize(provider); // Get the full hierarchy hierarchy = ((IProvisioningHierarchyFormatter)formatter).ToProvisioningHierarchy(mem); hierarchy.Connector = provider.Connector; } else if (packageFileName.EndsWith(".pnp", StringComparison.InvariantCultureIgnoreCase)) { // That's a PnP Package file // Get a provider based on the in-memory .PNP Open XML file OpenXMLConnector openXmlConnector = new OpenXMLConnector(mem); XMLTemplateProvider provider = new XMLOpenXMLTemplateProvider( openXmlConnector); // Get the .xml provisioning template file name var xmlTemplateFileName = openXmlConnector.Info?.Properties?.TemplateFileName ?? packageFileName.Substring(packageFileName.LastIndexOf('/') + 1) .ToLower().Replace(".pnp", ".xml"); // Get the full hierarchy hierarchy = provider.GetHierarchy(xmlTemplateFileName); hierarchy.Connector = provider.Connector; } #endregion #region Apply the template // Prepare variable to collect provisioned sites var provisionedSites = new List <Tuple <String, String> >(); // If we have a hierarchy with at least one Sequence if (hierarchy != null && hierarchy.Sequences != null && hierarchy.Sequences.Count > 0) { Console.WriteLine($"Provisioning hierarchy \"{hierarchy.DisplayName}\""); var tenantUrl = UrlUtilities.GetTenantAdministrationUrl(context.Url); // Retrieve the SPO Access Token var spoAdminAccessToken = await ProvisioningAppManager.AccessTokenProvider.GetAccessTokenAsync( tokenId, tenantUrl, ConfigurationManager.AppSettings[$"{action.ActionType}:ClientId"], ConfigurationManager.AppSettings[$"{action.ActionType}:ClientSecret"], ConfigurationManager.AppSettings[$"{action.ActionType}:AppUrl"]); log.WriteLine($"Retrieved target SharePoint Online Admin Center Access Token."); using (var tenantContext = authManager.GetAzureADAccessTokenAuthenticatedContext(tenantUrl, spoAdminAccessToken)) { using (var pnpTenantContext = PnPClientContext.ConvertFrom(tenantContext)) { var tenant = new Microsoft.Online.SharePoint.TenantAdministration.Tenant(pnpTenantContext); // Prepare a dictionary to hold the access tokens var accessTokens = new Dictionary <String, String>(); // Prepare logging for hierarchy application var ptai = new ProvisioningTemplateApplyingInformation(); ptai.MessagesDelegate += delegate(string message, ProvisioningMessageType messageType) { log.WriteLine($"{messageType} - {message}"); }; ptai.ProgressDelegate += delegate(string message, int step, int total) { log.WriteLine($"{step:00}/{total:00} - {message}"); }; ptai.SiteProvisionedDelegate += delegate(string title, string url) { log.WriteLine($"Fully provisioned site '{title}' with URL: {url}"); var provisionedSite = new Tuple <string, string>(title, url); if (!provisionedSites.Contains(provisionedSite)) { provisionedSites.Add(provisionedSite); } }; // Configure the OAuth Access Tokens for the client context accessTokens.Add(new Uri(tenantUrl).Authority, spoAdminAccessToken); accessTokens.Add(new Uri(spoTenant).Authority, spoAccessToken); // Configure the OAuth Access Tokens for the PnPClientContext, too pnpTenantContext.PropertyBag["AccessTokens"] = accessTokens; ptai.AccessTokens = accessTokens; #region Theme handling // Process the graphical Theme if (action.ApplyTheme) { // If we don't have any custom Theme if (!action.ApplyCustomTheme) { // Associate the selected already existing Theme to all the sites of the hierarchy foreach (var sc in hierarchy.Sequences[0].SiteCollections) { sc.Theme = action.SelectedTheme; foreach (var s in sc.Sites) { UpdateChildrenSitesTheme(s, action.SelectedTheme); } } } } #endregion // Configure provisioning parameters if (action.PackageProperties != null) { foreach (var key in action.PackageProperties.Keys) { if (hierarchy.Parameters.ContainsKey(key.ToString())) { hierarchy.Parameters[key.ToString()] = action.PackageProperties[key].ToString(); } else { hierarchy.Parameters.Add(key.ToString(), action.PackageProperties[key].ToString()); } // Configure telemetry properties telemetryProperties.Add($"PackageProperty.{key}", action.PackageProperties[key].ToString()); } } // Log telemetry event telemetry?.LogEvent("ProvisioningFunction.BeginProvisioning", telemetryProperties); // Define a PnPProvisioningContext scope to share the security context across calls using (var pnpProvisioningContext = new PnPProvisioningContext(async(r, s) => { if (accessTokens.ContainsKey(r)) { // In this scenario we just use the dictionary of access tokens // in fact the overall operation for sure will take less than 1 hour return(await Task.FromResult(accessTokens[r])); } else { // Try to get a fresh new Access Token var token = await ProvisioningAppManager.AccessTokenProvider.GetAccessTokenAsync( tokenId, $"https://{r}", ConfigurationManager.AppSettings[$"{action.ActionType}:ClientId"], ConfigurationManager.AppSettings[$"{action.ActionType}:ClientSecret"], ConfigurationManager.AppSettings[$"{action.ActionType}:AppUrl"]); accessTokens.Add(r, token); return(token); } })) { // Apply the hierarchy log.WriteLine($"Hierarchy Provisioning Started: {DateTime.Now:hh.mm.ss}"); tenant.ApplyProvisionHierarchy(hierarchy, hierarchy.Sequences[0].ID, ptai); log.WriteLine($"Hierarchy Provisioning Completed: {DateTime.Now:hh.mm.ss}"); } if (action.ApplyTheme && action.ApplyCustomTheme) { if (!String.IsNullOrEmpty(action.ThemePrimaryColor) && !String.IsNullOrEmpty(action.ThemeBodyTextColor) && !String.IsNullOrEmpty(action.ThemeBodyBackgroundColor)) { log.WriteLine($"Applying custom Theme to provisioned sites"); #region Palette generation for Theme var jsonPalette = ThemeUtility.GetThemeAsJSON( action.ThemePrimaryColor, action.ThemeBodyTextColor, action.ThemeBodyBackgroundColor); #endregion // Apply the custom theme to all of the provisioned sites foreach (var ps in provisionedSites) { using (var provisionedSiteContext = authManager.GetAzureADAccessTokenAuthenticatedContext(ps.Item2, spoAccessToken)) { if (provisionedSiteContext.Web.ApplyTheme(jsonPalette)) { log.WriteLine($"Custom Theme applied on site '{ps.Item1}' with URL: {ps.Item2}"); } else { log.WriteLine($"Failed to apply custom Theme on site '{ps.Item1}' with URL: {ps.Item2}"); } } } } } // Log telemetry event telemetry?.LogEvent("ProvisioningFunction.EndProvisioning", telemetryProperties); // Notify user about the provisioning outcome MailHandler.SendMailNotification( "ProvisioningCompleted", action.NotificationEmail, null, new { TemplateName = action.DisplayName, ProvisionedSites = provisionedSites, }, appOnlyAccessToken); // Log reporting event (1 = Success) logReporting(action, provisioningEnvironment, startProvisioning, package, 1); } } } else { throw new ApplicationException($"The requested package does not contain a valid PnP Hierarchy!"); } #endregion } else { throw new ApplicationException($"Cannot find the package with ID: {action.PackageId}"); } #endregion log.WriteLine($"Function successfully executed!"); // Log telemetry event telemetry?.LogEvent("ProvisioningFunction.End", telemetryProperties); } } else { var noTokensErrorMessage = $"Cannot retrieve Refresh Token or Access Token for {action.UserPrincipalName} in tenant {action.TenantId}!"; log.WriteLine(noTokensErrorMessage); throw new ApplicationException(noTokensErrorMessage); } } catch (Exception ex) { // Log telemetry event telemetry?.LogException(ex, "ProvisioningFunction.RunAsync", telemetryProperties); // Notify user about the provisioning outcome MailHandler.SendMailNotification( "ProvisioningFailed", action.NotificationEmail, null, new { TemplateName = action.DisplayName, ExceptionDetails = SimplifyException(ex), PnPCorrelationId = action.CorrelationId.ToString(), }, appOnlyAccessToken); // Log reporting event (2 = Failed) logReporting(action, provisioningEnvironment, startProvisioning, null, 2, ex.ToDetailedString()); throw ex; } finally { telemetry?.Flush(); } }
public static int DeleteSite(int itemId) { // check account int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo); if (accountCheck < 0) { return(accountCheck); } // load original meta item SharePointSite origItem = (SharePointSite)PackageController.GetPackageItem(itemId); if (origItem == null) { return(BusinessErrorCodes.ERROR_SHAREPOINT_PACKAGE_ITEM_NOT_FOUND); } // place log record TaskManager.StartTask("SHAREPOINT", "DELETE_SITE", origItem.Name, itemId); try { // get service SharePointServer sps = GetSharePoint(origItem.ServiceId); // delete service item sps.UnextendVirtualServer(origItem.Name, true); try { // delete database ServiceProviderItem dbItem = PackageController.GetPackageItemByName(origItem.PackageId, origItem.DatabaseGroupName, origItem.DatabaseName, typeof(SqlDatabase)); if (dbItem != null) { DatabaseServerController.DeleteSqlDatabase(dbItem.Id); } // delete database user dbItem = PackageController.GetPackageItemByName(origItem.PackageId, origItem.DatabaseGroupName, origItem.DatabaseUser, typeof(SqlUser)); if (dbItem != null) { DatabaseServerController.DeleteSqlUser(dbItem.Id); } } catch (Exception ex2) { TaskManager.WriteError(ex2); } // delete meta item PackageController.DeletePackageItem(origItem.Id); try { // start web site WebSite site = WebServerController.GetWebSite(origItem.PackageId, origItem.Name); if (site != null) { WebServerController.ChangeSiteState(site.Id, ServerState.Started); } } catch (Exception ex) { TaskManager.WriteError(ex); } return(0); } catch (Exception ex) { throw TaskManager.WriteError(ex); } finally { TaskManager.CompleteTask(); } }
private static async Task LoadThemesFromTenant(ProvisioningActionModel model, string tokenId, SharePointSite rootSite, string graphAccessToken) { // Retrieve the SPO URL for the Admin Site var adminSiteUrl = rootSite.WebUrl.Replace(".sharepoint.com", "-admin.sharepoint.com"); // Retrieve the SPO Access Token var spoAccessToken = await ProvisioningAppManager.AccessTokenProvider.GetAccessTokenAsync( tokenId, adminSiteUrl, ConfigurationManager.AppSettings["ida:ClientId"], ConfigurationManager.AppSettings["ida:ClientSecret"], ConfigurationManager.AppSettings["ida:AppUrl"]); // Connect to SPO and retrieve the list of available Themes AuthenticationManager authManager = new AuthenticationManager(); using (ClientContext spoContext = authManager.GetAzureADAccessTokenAuthenticatedContext(adminSiteUrl, spoAccessToken)) { TenantAdmin.Tenant tenant = new TenantAdmin.Tenant(spoContext); var themes = tenant.GetAllTenantThemes(); spoContext.Load(themes); spoContext.ExecuteQueryRetry(); model.Themes = themes.Select(t => t.Name).ToList(); } }
public async Task <ActionResult> Provision(String packageId = null, String returnUrl = null) { if (String.IsNullOrEmpty(packageId)) { throw new ArgumentNullException("packageId"); } CheckBetaFlag(); PrepareHeaderData(returnUrl); ProvisioningActionModel model = new ProvisioningActionModel(); if (IsValidUser()) { var issuer = (System.Threading.Thread.CurrentPrincipal as System.Security.Claims.ClaimsPrincipal)?.FindFirst("iss"); if (issuer != null && !String.IsNullOrEmpty(issuer.Value)) { var issuerValue = issuer.Value.Substring(0, issuer.Value.Length - 1); var tenantId = issuerValue.Substring(issuerValue.LastIndexOf("/") + 1); var upn = (System.Threading.Thread.CurrentPrincipal as System.Security.Claims.ClaimsPrincipal)?.FindFirst(ClaimTypes.Upn)?.Value; if (this.IsAllowedUpnTenant(upn)) { #region Prepare model generic context data // Prepare the model data model.TenantId = tenantId; model.UserPrincipalName = upn; model.PackageId = packageId; model.ApplyTheme = false; model.ApplyCustomTheme = false; String provisioningScope = ConfigurationManager.AppSettings["SPPA:ProvisioningScope"]; String provisioningEnvironment = ConfigurationManager.AppSettings["SPPA:ProvisioningEnvironment"]; var tokenId = $"{model.TenantId}-{model.UserPrincipalName.GetHashCode()}-{provisioningScope}-{provisioningEnvironment}"; var graphAccessToken = await ProvisioningAppManager.AccessTokenProvider.GetAccessTokenAsync( tokenId, "https://graph.microsoft.com/"); model.UserIsTenantAdmin = Utilities.UserIsTenantGlobalAdmin(graphAccessToken); model.UserIsSPOAdmin = Utilities.UserIsSPOAdmin(graphAccessToken); model.NotificationEmail = upn; model.ReturnUrl = returnUrl; #endregion // Determine the URL of the root SPO site for the current tenant var rootSiteJson = HttpHelper.MakeGetRequestForString("https://graph.microsoft.com/v1.0/sites/root", graphAccessToken); SharePointSite rootSite = JsonConvert.DeserializeObject <SharePointSite>(rootSiteJson); // Store the SPO Root Site URL in the Model model.SPORootSiteUrl = rootSite.WebUrl; // If the current user is an admin, we can get the available Themes if (model.UserIsTenantAdmin || model.UserIsSPOAdmin) { await LoadThemesFromTenant(model, tokenId, rootSite, graphAccessToken); } LoadPackageDataIntoModel(packageId, model); } else { throw new ApplicationException("Invalid request, the current tenant is not allowed to use this solution!"); } } } return(View("Provision", model)); }
/// <summary> /// This method first connects to the Organization service. Afterwards, /// create, retrieve, update, and delete operations are performed on the /// SharePoint location records. /// </summary> /// <param name="serverConfig">Contains server connection information.</param> /// <param name="promptforDelete">When True, the user will be prompted to delete all /// created entities.</param> public void Run(ServerConnection.Configuration serverConfig, bool promptforDelete) { try { //<snippetCRUDSharePointLocationRecords1> // Connect to the Organization service. // The using statement assures that the service proxy will be properly disposed. using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri,serverConfig.Credentials, serverConfig.DeviceCredentials)) { // This statement is required to enable early-bound type support. _serviceProxy.EnableProxyTypes(); CreateRequiredRecords(); //<snippetCRUDSharePointLocationRecords2> // Instantiate a SharePoint site object. // See the Entity Metadata topic in the SDK documentation to determine // which attributes must be set for each entity. SharePointSite spSite = new SharePointSite { Name = "Sample SharePoint Site", Description = "Sample SharePoint Site Location record", // TODO: Change this URL to a valid SharePoint URL. AbsoluteURL = "http://www.example.com", }; // Create a SharePoint site record named Sample SharePoint Site. _spSiteId = _serviceProxy.Create(spSite); //</snippetCRUDSharePointLocationRecords2> Console.WriteLine("{0} created.", spSite.Name); //<snippetCRUDSharePointLocationRecords3> // Instantiate a SharePoint document location object. // See the Entity Metadata topic in the SDK documentation to determine // which attributes must be set for each entity. SharePointDocumentLocation spDocLoc = new SharePointDocumentLocation { Name = "Sample SharePoint Document Location", Description = "Sample SharePoint Document Location record", // Set the Sample SharePoint Site created earlier as the parent site. ParentSiteOrLocation = new EntityReference(SharePointSite.EntityLogicalName, _spSiteId), RelativeUrl = "spdocloc", // Associate this document location instance with the Fourth Coffee // sample account record. RegardingObjectId = new EntityReference(Account.EntityLogicalName, _account1Id) }; // Create a SharePoint document location record named Sample SharePoint Document Location. _spDocLocId = _serviceProxy.Create(spDocLoc); //</snippetCRUDSharePointLocationRecords3> Console.WriteLine("{0} created.", spDocLoc.Name); // Retrieve the SharePoint site and SharePoint document location containing several of its attributes. ColumnSet colsSpSite = new ColumnSet("name", "absoluteurl"); SharePointSite retrievedSpSite = (SharePointSite)_serviceProxy.Retrieve(SharePointSite.EntityLogicalName, _spSiteId, colsSpSite); ColumnSet colsSpDocLoc = new ColumnSet("name", "regardingobjectid"); SharePointDocumentLocation retrievedSpDocLoc = (SharePointDocumentLocation)_serviceProxy.Retrieve(SharePointDocumentLocation.EntityLogicalName, _spDocLocId, colsSpDocLoc); Console.Write("Retrieved,"); // Update the URL of the SharePoint site. // TODO: Change this URL to a valid SharePoint URL. retrievedSpSite.AbsoluteURL = "http://www.example.net"; _serviceProxy.Update(retrievedSpSite); // Update the SharePoint document location to associate it with the // Northwind Traders sample account. retrievedSpDocLoc.RegardingObjectId = new EntityReference(Account.EntityLogicalName,_account2Id); _serviceProxy.Update(retrievedSpDocLoc); Console.WriteLine(" and updated the records."); DeleteRequiredRecords(promptforDelete); } //</snippetCRUDSharePointLocationRecords1> } // Catch any service fault exceptions that Microsoft Dynamics CRM throws. catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>) { // You can handle an exception here or pass it back to the calling method. throw; } }
// GET: SharePointSite public async Task <ActionResult> Index() { SharePointSite site = await SharePointSiteManager.GetSharePointSite(); return(View(site)); }
public ActionResult Index() { SharePointSite site = SharePointSiteManager.GetSharePointSite(); return(View(site)); }
/// <summary> /// This method first connects to the Organization service. Afterwards, /// create, retrieve, update, and delete operations are performed on the /// SharePoint location records. /// </summary> /// <param name="serverConfig">Contains server connection information.</param> /// <param name="promptforDelete">When True, the user will be prompted to delete all /// created entities.</param> public void Run(ServerConnection.Configuration serverConfig, bool promptforDelete) { try { //<snippetCRUDSharePointLocationRecords1> // Connect to the Organization service. // The using statement assures that the service proxy will be properly disposed. using (_serviceProxy = ServerConnection.GetOrganizationProxy(serverConfig)) { // This statement is required to enable early-bound type support. _serviceProxy.EnableProxyTypes(); CreateRequiredRecords(); //<snippetCRUDSharePointLocationRecords2> // Instantiate a SharePoint site object. // See the Entity Metadata topic in the SDK documentation to determine // which attributes must be set for each entity. SharePointSite spSite = new SharePointSite { Name = "Sample SharePoint Site", Description = "Sample SharePoint Site Location record", // TODO: Change this URL to a valid SharePoint URL. AbsoluteURL = "http://www.example.com", }; // Create a SharePoint site record named Sample SharePoint Site. _spSiteId = _serviceProxy.Create(spSite); //</snippetCRUDSharePointLocationRecords2> Console.WriteLine("{0} created.", spSite.Name); //<snippetCRUDSharePointLocationRecords3> // Instantiate a SharePoint document location object. // See the Entity Metadata topic in the SDK documentation to determine // which attributes must be set for each entity. SharePointDocumentLocation spDocLoc = new SharePointDocumentLocation { Name = "Sample SharePoint Document Location", Description = "Sample SharePoint Document Location record", // Set the Sample SharePoint Site created earlier as the parent site. ParentSiteOrLocation = new EntityReference(SharePointSite.EntityLogicalName, _spSiteId), RelativeUrl = "spdocloc", // Associate this document location instance with the Fourth Coffee // sample account record. RegardingObjectId = new EntityReference(Account.EntityLogicalName, _account1Id) }; // Create a SharePoint document location record named Sample SharePoint Document Location. _spDocLocId = _serviceProxy.Create(spDocLoc); //</snippetCRUDSharePointLocationRecords3> Console.WriteLine("{0} created.", spDocLoc.Name); // Retrieve the SharePoint site and SharePoint document location containing several of its attributes. ColumnSet colsSpSite = new ColumnSet("name", "absoluteurl"); SharePointSite retrievedSpSite = (SharePointSite)_serviceProxy.Retrieve(SharePointSite.EntityLogicalName, _spSiteId, colsSpSite); ColumnSet colsSpDocLoc = new ColumnSet("name", "regardingobjectid"); SharePointDocumentLocation retrievedSpDocLoc = (SharePointDocumentLocation)_serviceProxy.Retrieve(SharePointDocumentLocation.EntityLogicalName, _spDocLocId, colsSpDocLoc); Console.Write("Retrieved,"); // Update the URL of the SharePoint site. // TODO: Change this URL to a valid SharePoint URL. retrievedSpSite.AbsoluteURL = "http://www.example.net"; _serviceProxy.Update(retrievedSpSite); // Update the SharePoint document location to associate it with the // Northwind Traders sample account. retrievedSpDocLoc.RegardingObjectId = new EntityReference(Account.EntityLogicalName, _account2Id); _serviceProxy.Update(retrievedSpDocLoc); Console.WriteLine(" and updated the records."); DeleteRequiredRecords(promptforDelete); } //</snippetCRUDSharePointLocationRecords1> } // Catch any service fault exceptions that Microsoft Dynamics CRM throws. catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> ) { // You can handle an exception here or pass it back to the calling method. throw; } }
public int AddSharePointSite(SharePointSite item) { return(SharePointServerController.AddSite(item)); }
/// <summary> /// Creates any entity records that this sample requires. /// </summary> public void CreateRequiredRecords() { // Instantiate a SharePoint site object. // See the Entity Metadata topic in the SDK documentation to determine // which attributes must be set for each entity. SharePointSite spSite = new SharePointSite { Name = "Sample SharePoint Site", Description = "Sample SharePoint Site Location record", AbsoluteURL = _siteAbsoluteURL, IsGridPresent = true }; // Create a SharePoint site record named Sample SharePoint Site. _spSiteId = _serviceProxy.Create(spSite); Console.WriteLine("{0} created.", spSite.Name); // Instantiate a SharePoint document location object. // See the Entity Metadata topic in the SDK documentation to determine // which attributes must be set for each entity. SharePointDocumentLocation spDocLoc = new SharePointDocumentLocation { Name = "Sample SharePoint Document Location", Description = "Sample SharePoint Document Location record", // Set the Sample SharePoint Site created earlier as the parent site. ParentSiteOrLocation = new EntityReference(SharePointSite.EntityLogicalName, _spSiteId), RelativeUrl = "spdocloc" }; // Create a SharePoint document location record named Sample SharePoint Document Location. _spDocLocId = _serviceProxy.Create(spDocLoc); Console.WriteLine("{0} created.", spDocLoc.Name); }
public string Executar(string mensagem, string numeroMensagem, Domain.Model.Usuario usuario) { //usuarioIntegracao = usuario; var xml = this.CarregarMensagem <Pollux.MSG0125>(mensagem); if (!String.IsNullOrEmpty(xml.CodigoObjeto) && xml.CodigoObjeto.Length == 36) { if (String.IsNullOrEmpty(xml.TipoObjeto)) { resultadoConsulta.Sucesso = false; resultadoConsulta.Mensagem = "TipoObjeto não enviado."; retorno.Add("Resultado", resultadoConsulta); return(CriarMensagemRetorno <Pollux.MSG0114R1>(numeroMensagem, retorno)); } //Obter endereço do servidor (urlAbsoluta) SharePointSite objSharePoint = new SharePointSite(this.Organizacao, this.IsOffline); objSharePoint = new Servicos.SharePointSiteService(this.Organizacao, this.IsOffline).ObterPorUrlRelativa(); if (objSharePoint == null) { resultadoConsulta.Sucesso = false; resultadoConsulta.Mensagem = "Site Sharepoint não encontrado no Crm."; retorno.Add("Resultado", resultadoConsulta); return(CriarMensagemRetorno <Pollux.MSG0125R1>(numeroMensagem, retorno)); } //Obter a pasta da conta List <DocumentoSharePoint> lstDocSharePoint = new Servicos.SharePointSiteService(this.Organizacao, this.IsOffline).ListarPorIdRegistro(new Guid(xml.CodigoObjeto)); foreach (var item in lstDocSharePoint) { Pollux.Entities.ArquivoItem arqItem = new Pollux.Entities.ArquivoItem(); if (item != null && (!String.IsNullOrEmpty(item.UrlRelativa)) && !String.IsNullOrEmpty(objSharePoint.UrlAbsoluta)) { arqItem.Nome = item.Nome; arqItem.URL = objSharePoint.UrlAbsoluta + "/" + xml.TipoObjeto + "/" + item.UrlRelativa; lstArquivoItens.Add(arqItem); } else { resultadoConsulta.Sucesso = true; resultadoConsulta.Mensagem = "Dados do SharePoint no Crm não encontrados."; retorno.Add("Resultado", resultadoConsulta); return(CriarMensagemRetorno <Pollux.MSG0125R1>(numeroMensagem, retorno)); } } } else { resultadoConsulta.Sucesso = false; resultadoConsulta.Mensagem = "Identificador da Entidade (Guid) não enviado/fora do padrão."; retorno.Add("Resultado", resultadoConsulta); return(CriarMensagemRetorno <Pollux.MSG0125R1>(numeroMensagem, retorno)); } if (lstArquivoItens.Count == 0) { resultadoConsulta.Sucesso = true; resultadoConsulta.Mensagem = "Documentos não encontrados."; retorno.Add("Resultado", resultadoConsulta); return(CriarMensagemRetorno <Pollux.MSG0125R1>(numeroMensagem, retorno)); } resultadoConsulta.Sucesso = true; resultadoConsulta.Mensagem = "Integração ocorrida com sucesso"; retorno.Add("Resultado", resultadoConsulta); retorno.Add("ArquivoItems", lstArquivoItens); return(CriarMensagemRetorno <Pollux.MSG0125R1>(numeroMensagem, retorno)); }