コード例 #1
0
        public PublishedWorkbookResult PublishWorkbookWithEmbeddedCredentials(PublishWorkbookRequest publishRequest)
        {
            PublishedWorkbookResult publishedWorkbookResult = new PublishedWorkbookResult(publishRequest);

            // Construct URI & compose request payload.
            var       uri            = Endpoints.GetPublishWorkbookUri(baseUri, publishRequest.SiteId, publishRequest.OverwriteExistingWorkbook);
            tsRequest requestPayload = new tsRequest
            {
                Item = new workbookType
                {
                    name              = Path.GetFileNameWithoutExtension(publishRequest.FilePath),
                    showTabs          = publishRequest.ShowSheetsAsTabs,
                    showTabsSpecified = publishRequest.ShowSheetsAsTabs,
                    project           = new projectType
                    {
                        id = publishRequest.ProjectId
                    },
                    connectionCredentials = new connectionCredentialsType
                    {
                        name           = publishRequest.DatasourceUserName,
                        password       = publishRequest.DatasourcePassword,
                        embed          = true,
                        embedSpecified = true
                    }
                }
            };

            // Construct multipart request body using a boundary string to delimit sections.
            var    boundaryString = Guid.NewGuid().ToString().Replace("-", "");
            string contentType    = String.Format("multipart/mixed; boundary={0}", boundaryString);

            byte[] requestBody = PublishRequestBuilder.BuildRequestBody(publishRequest.FilePath, requestPayload, boundaryString);

            // Issue request.
            var        errorMessage = String.Format("Failed to publish workbook '{0}'", publishRequest.WorkbookName);
            ApiRequest apiRequest   = new ApiRequest(uri, HttpMethod.Post, GetAuthToken(), headers: null, contentType: contentType, body: requestBody, timeoutSeconds: publishRequest.publishingTimeoutSeconds);

            try
            {
                tsResponse response = apiRequest.TryIssueRequest(errorMessage);

                publishedWorkbookResult.IsSuccessful = true;
                publishedWorkbookResult.WorkbookId   = response.GetWorkbook().id;
                publishedWorkbookResult.Uri          = GetWorkbookUrl(response.GetWorkbook().contentUrl);
            }
            catch (Exception ex)
            {
                publishedWorkbookResult.IsSuccessful = false;
                publishedWorkbookResult.ErrorMessage = ex.Message;
            }

            // Add any tags to the newly-published workbook.
            if (publishedWorkbookResult.IsSuccessful)
            {
                try
                {
                    AddTagsToWorkbook(publishedWorkbookResult.WorkbookId, publishRequest.Tags);
                }
                catch
                {
                    // We swallow any errors here.
                }
            }

            return(publishedWorkbookResult);
        }