Example #1
0
        private static Entity GetDocumentLocation(OrganizationServiceContext context, Entity entity, EntityMetadata entityMetadata, Entity spSite)
        {
            var locations = context.CreateQuery(SharePointDocumentLocationLogicalName)
                            .Where(docLoc => docLoc.GetAttributeValue <EntityReference>("regardingobjectid").Id == entity.Id && docLoc.GetAttributeValue <int>("statecode") == 0)
                            .OrderBy(docLoc => docLoc.GetAttributeValue <DateTime>("createdon"))
                            .ToArray();

            Entity location;

            if (locations.Count() > 1)
            {
                // Multiple doc locations found, choose the first created.
                location = locations.First();
            }
            else if (locations.Count() == 1)
            {
                location = locations.First();
            }
            else
            {
                // No document locations found, create an auto-generated one.
                var autoGeneratedRelativeUrl = "{0}_{1}".FormatWith(
                    entity.GetAttributeValue <string>(entityMetadata.PrimaryNameAttribute),
                    entity.Id.ToString("N").ToUpper());

                location = context.AddOrGetExistingDocumentLocationAndSave <Entity>(spSite, entity, autoGeneratedRelativeUrl);
            }

            if (location == null)
            {
                throw new Exception("A document location couldn't be found or created for the entity.");
            }

            return(location);
        }
        private static Entity GetDocumentLocation(OrganizationServiceContext context, Entity entity, string folderName)
        {
            var spConnection = new SharePointConnection("SharePoint");
            var spSite       = context.GetSharePointSiteFromUrl(spConnection.Url);

            var entityPermissionProvider = new CrmEntityPermissionProvider();
            var result = new SharePointResult(entity.ToEntityReference(), entityPermissionProvider, context);

            if (!result.PermissionsExist || !result.CanCreate || !result.CanAppend || !result.CanAppendTo)
            {
                return(null);
            }

            return(context.AddOrGetExistingDocumentLocationAndSave <Entity>(spSite, entity, folderName));
        }