Example #1
0
        public ISharePointResult AddFiles(EntityReference regarding, IList <HttpPostedFileBase> files, bool overwrite = true, string folderPath = null)
        {
            var context = _dependencies.GetServiceContextForWrite();
            var entityPermissionProvider = new CrmEntityPermissionProvider();
            var result = new SharePointResult(regarding, entityPermissionProvider, context);

            if (files == null || !files.Any())
            {
                return(result);
            }

            var entityMetadata = context.GetEntityMetadata(regarding.LogicalName);
            var entity         = context.CreateQuery(regarding.LogicalName).First(e => e.GetAttributeValue <Guid>(entityMetadata.PrimaryIdAttribute) == regarding.Id);

            // assert permission to create the sharepointdocumentlocation entity
            if (!result.PermissionsExist || !result.CanCreate || !result.CanAppend || !result.CanAppendTo)
            {
                ADXTrace.Instance.TraceInfo(TraceCategory.Application, "Permission Denied. You do not have the appropriate Entity Permissions to Create or Append document locations or AppendTo the regarding entity.");
                return(result);
            }

            var spConnection = new SharePointConnection(SharePointConnectionStringName);
            var spSite       = context.GetSharePointSiteFromUrl(spConnection.Url);

            var location = GetDocumentLocation(context, entity, entityMetadata, spSite);

            // assert permission to write the sharepointdocumentlocation entity
            if (!result.CanWrite)
            {
                ADXTrace.Instance.TraceInfo(TraceCategory.Application, "Permission Denied. You do not have the appropriate Entity Permissions to Write document locations.");
                return(result);
            }

            var factory = new ClientFactory();

            using (var client = factory.CreateClientContext(spConnection))
            {
                // retrieve the SharePoint list and folder names for the document location
                string listUrl, folderUrl;

                context.GetDocumentLocationListAndFolder(location, out listUrl, out folderUrl);

                var folder = client.AddOrGetExistingFolder(listUrl, folderUrl + folderPath);

                foreach (var postedFile in files)
                {
                    using (var file = postedFile.InputStream)
                    {
                        // upload a file to the folder
                        client.SaveFile(file, folder, Path.GetFileName(postedFile.FileName), overwrite);
                    }
                }
            }

            return(result);
        }
        /// <summary>
        /// Creates a Portal Comment entity, as well Annotation entities for any attachments
        /// </summary>
        /// <param name="portalComment"></param>
        /// <returns></returns>
        public PortalCommentCreateResult CreatePortalComment(PortalComment portalComment)
        {
            var serviceContext         = _dependencies.GetServiceContext();
            var serviceContextForWrite = _dependencies.GetServiceContextForWrite();

            PortalCommentCreateResult result = null;

            var entityPermissionProvider = new CrmEntityPermissionProvider();

            result = new PortalCommentCreateResult(entityPermissionProvider, serviceContext, portalComment.Regarding);

            if (result.PermissionsExist && result.PermissionGranted)
            {
                var acceptMimeTypes      = AnnotationDataAdapter.GetAcceptRegex(portalComment.AttachmentSettings.AcceptMimeTypes);
                var acceptExtensionTypes = AnnotationDataAdapter.GetAcceptRegex(portalComment.AttachmentSettings.AcceptExtensionTypes);
                if (portalComment.FileAttachments != null)
                {
                    portalComment.FileAttachments.ForEach(attachment =>
                    {
                        if (!(acceptExtensionTypes.IsMatch(Path.GetExtension(attachment.FileName).ToLower()) ||
                              acceptMimeTypes.IsMatch(attachment.MimeType)))
                        {
                            throw new AnnotationException(portalComment.AttachmentSettings.RestrictMimeTypesErrorMessage);
                        }
                    });
                }

                var owner = portalComment.To?.GetAttributeValue <EntityReference>("partyid");

                var entity = new Entity("adx_portalcomment");

                entity.SetAttributeValue("description", portalComment.Description);
                entity.SetAttributeValue("regardingobjectid", portalComment.Regarding);
                entity.SetAttributeValue("regardingobjecttypecode", portalComment.Regarding.LogicalName);
                entity.SetAttributeValue("from", new Entity[] { portalComment.From });
                entity.SetAttributeValue("adx_portalcommentdirectioncode", new OptionSetValue((int)portalComment.DirectionCode));

                if (owner != null)
                {
                    entity.SetAttributeValue("ownerid", owner);

                    if (!string.Equals(owner.LogicalName, "team", StringComparison.OrdinalIgnoreCase))
                    {
                        entity.SetAttributeValue("to", new Entity[] { portalComment.To });
                    }
                }

                // Create adx_portalcomment but skip cache invalidation.
                var id = (serviceContext as IOrganizationService).ExecuteCreate(entity, RequestFlag.ByPassCacheInvalidation);

                portalComment.ActivityId = entity.Id = id;
                portalComment.Entity     = entity;

                // Can only change state code value after entity creation
                entity.SetAttributeValue("statecode", new OptionSetValue((int)portalComment.StateCode));
                entity.SetAttributeValue("statuscode", new OptionSetValue((int)portalComment.StatusCode));

                // Explicitly include the activityid, this way the Cache will know to add dependency on "activitypointer" for this "adx_portalcomment" entity.
                entity.SetAttributeValue("activityid", id);

                (serviceContext as IOrganizationService).ExecuteUpdate(entity);

                if (portalComment.FileAttachments != null)
                {
                    // permission for Portal Comment implies permission for attachment to Portal Comment
                    portalComment.AttachmentSettings.RespectPermissions = false;

                    foreach (IAnnotationFile attachment in portalComment.FileAttachments)
                    {
                        IAnnotation annotation = new Annotation
                        {
                            Subject        = String.Empty,
                            NoteText       = String.Empty,
                            Regarding      = portalComment.Entity.ToEntityReference(),
                            FileAttachment = attachment
                        };

                        IAnnotationDataAdapter da = new AnnotationDataAdapter(_dependencies);
                        da.CreateAnnotation(annotation, portalComment.AttachmentSettings);
                    }
                }
            }

            return(result);
        }