private async Task CreateAccountDocumentLocation(MicrosoftDynamicsCRMaccount account)
        {
            string serverRelativeUrl = account.GetServerUrl(_sharePointFileManager);
            string name = "";

            if (string.IsNullOrEmpty(account.BcgovDoingbusinessasname))
            {
                name = account.Accountid;
            }
            else
            {
                name = account.BcgovDoingbusinessasname;
            }

            name += " Account Files";


            // create a SharePointDocumentLocation link
            string folderName = "_" + account.Accountid;


            // Create the folder
            bool folderExists = await _sharePointFileManager.FolderExists(SharePointFileManager.AccountDocumentListTitle, folderName);

            if (!folderExists)
            {
                try
                {
                    var folder = await _sharePointFileManager.CreateFolder(SharePointFileManager.AccountDocumentListTitle, folderName);
                }
                catch (Exception e)
                {
                    _logger.LogError("Error creating Sharepoint Folder");
                    _logger.LogError($"List is: {SharePointFileManager.AccountDocumentListTitle}");
                    _logger.LogError($"FolderName is: {folderName}");
                    throw e;
                }
            }

            // now create a document location to link them.

            // Create the SharePointDocumentLocation entity
            MicrosoftDynamicsCRMsharepointdocumentlocation mdcsdl = new MicrosoftDynamicsCRMsharepointdocumentlocation()
            {
                Relativeurl = folderName,
                Description = "Account Files",
                Name        = name
            };


            try
            {
                mdcsdl = _dynamicsClient.Sharepointdocumentlocations.Create(mdcsdl);
            }
            catch (OdataerrorException odee)
            {
                _logger.LogError("Error creating SharepointDocumentLocation");
                _logger.LogError("Request:");
                _logger.LogError(odee.Request.Content);
                _logger.LogError("Response:");
                _logger.LogError(odee.Response.Content);
                mdcsdl = null;
            }
            if (mdcsdl != null)
            {
                // set the parent document library.
                string parentDocumentLibraryReference = GetDocumentLocationReferenceByRelativeURL("account");

                string accountUri = _dynamicsClient.GetEntityURI("accounts", account.Accountid);
                // add a regardingobjectid.
                var patchSharePointDocumentLocationIncident = new MicrosoftDynamicsCRMsharepointdocumentlocation()
                {
                    RegardingobjectIdAccountODataBind = accountUri,
                    ParentsiteorlocationSharepointdocumentlocationODataBind = _dynamicsClient.GetEntityURI("sharepointdocumentlocations", parentDocumentLibraryReference),
                    Relativeurl = folderName,
                    Description = "Account Files",
                };

                try
                {
                    _dynamicsClient.Sharepointdocumentlocations.Update(mdcsdl.Sharepointdocumentlocationid, patchSharePointDocumentLocationIncident);
                }
                catch (OdataerrorException odee)
                {
                    _logger.LogError("Error adding reference SharepointDocumentLocation to account");
                    _logger.LogError("Request:");
                    _logger.LogError(odee.Request.Content);
                    _logger.LogError("Response:");
                    _logger.LogError(odee.Response.Content);
                }

                string sharePointLocationData = _dynamicsClient.GetEntityURI("sharepointdocumentlocations", mdcsdl.Sharepointdocumentlocationid);

                OdataId oDataId = new OdataId()
                {
                    OdataIdProperty = sharePointLocationData
                };
                try
                {
                    _dynamicsClient.Accounts.AddReference(account.Accountid, "Account_SharepointDocumentLocation", oDataId);
                }
                catch (OdataerrorException odee)
                {
                    _logger.LogError("Error adding reference to SharepointDocumentLocation");
                    _logger.LogError("Request:");
                    _logger.LogError(odee.Request.Content);
                    _logger.LogError("Response:");
                    _logger.LogError(odee.Response.Content);
                }
            }
        }
Пример #2
0
        private async Task CreateAccountDocumentLocation(IDynamicsClient _dynamicsClient, FileManagerClient _fileManagerClient, MicrosoftDynamicsCRMaccount account)
        {
            string name = "";

            try
            {
                string serverRelativeUrl = account.GetServerUrl();

                if (string.IsNullOrEmpty(account.Name))
                {
                    name = account.Accountid;
                }
                else
                {
                    name = account.Name;
                }

                name += " Account Files";

                string folderName = $"{account.Name}_{account.Accountid}";


                var createFolderRequest = new CreateFolderRequest()
                {
                    EntityName = "account",
                    FolderName = folderName
                };

                var createFolderResult = _fileManagerClient.CreateFolder(createFolderRequest);

                if (createFolderResult.ResultStatus == ResultStatus.Fail)
                {
                    _logger.LogError($"Error creating folder for account {name}. Error is {createFolderResult.ErrorDetail}");
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"Error creating folder for account {name}");
            }


            /*
             * // now create a document location to link them.
             *
             * // Create the SharePointDocumentLocation entity
             * MicrosoftDynamicsCRMsharepointdocumentlocation mdcsdl = new MicrosoftDynamicsCRMsharepointdocumentlocation()
             * {
             *  Relativeurl = folderName,
             *  Description = "Account Files",
             *  Name = name
             * };
             *
             *
             * try
             * {
             *  mdcsdl = _dynamicsClient.Sharepointdocumentlocations.Create(mdcsdl);
             * }
             * catch (OdataerrorException odee)
             * {
             *  _logger.LogError("Error creating SharepointDocumentLocation");
             *  _logger.LogError("Request:");
             *  _logger.LogError(odee.Request.Content);
             *  _logger.LogError("Response:");
             *  _logger.LogError(odee.Response.Content);
             *  mdcsdl = null;
             * }
             * if (mdcsdl != null)
             * {
             *
             *  // set the parent document library.
             *  string parentDocumentLibraryReference = GetDocumentLocationReferenceByRelativeURL("account");
             *
             *  string accountUri = _dynamicsClient.GetEntityURI("accounts", account.Accountid);
             *  // add a regardingobjectid.
             *  var patchSharePointDocumentLocationIncident = new MicrosoftDynamicsCRMsharepointdocumentlocation()
             *  {
             *      RegardingobjectIdAccountODataBind = accountUri,
             *      ParentsiteorlocationSharepointdocumentlocationODataBind = _dynamicsClient.GetEntityURI("sharepointdocumentlocations", parentDocumentLibraryReference),
             *      Relativeurl = folderName,
             *      Description = "Account Files",
             *  };
             *
             *  try
             *  {
             *      _dynamicsClient.Sharepointdocumentlocations.Update(mdcsdl.Sharepointdocumentlocationid, patchSharePointDocumentLocationIncident);
             *  }
             *  catch (OdataerrorException odee)
             *  {
             *      _logger.LogError("Error adding reference SharepointDocumentLocation to account");
             *      _logger.LogError("Request:");
             *      _logger.LogError(odee.Request.Content);
             *      _logger.LogError("Response:");
             *      _logger.LogError(odee.Response.Content);
             *  }
             *
             *  string sharePointLocationData = _dynamicsClient.GetEntityURI("sharepointdocumentlocations", mdcsdl.Sharepointdocumentlocationid);
             *
             *  OdataId oDataId = new OdataId()
             *  {
             *      OdataIdProperty = sharePointLocationData
             *  };
             *  try
             *  {
             *      _dynamicsClient.Accounts.AddReference(account.Accountid, "Account_SharepointDocumentLocation", oDataId);
             *  }
             *  catch (OdataerrorException odee)
             *  {
             *      _logger.LogError("Error adding reference to SharepointDocumentLocation");
             *      _logger.LogError("Request:");
             *      _logger.LogError(odee.Request.Content);
             *      _logger.LogError("Response:");
             *      _logger.LogError(odee.Response.Content);
             *  }
             * }
             */
        }