private static IForumPostInfo GetForumPostInfo(string alias, Entity entity, IForumAuthor author) { if (entity == null) { throw new ArgumentNullException("entity"); } if (author == null) { throw new ArgumentNullException("author"); } IForumPostInfo unknownLatestPostInfo = new UnknownForumPostInfo(); var latestPostId = entity.GetAttributeAliasedValue <Guid?>("{0}.adx_communityforumpostid".FormatWith(alias)); return(latestPostId == null ? unknownLatestPostInfo : new ForumPostInfo( new EntityReference("adx_communityforumpost", latestPostId.Value), author, entity.GetAttributeAliasedValue <DateTime?>("{0}.adx_date".FormatWith(alias)).GetValueOrDefault(unknownLatestPostInfo.PostedOn))); }
public static IDictionary <Guid, IForumPostInfo> FetchForumPostInfos(this OrganizationServiceContext serviceContext, IEnumerable <Guid> forumPostIds, Guid websiteId, CloudBlobContainer cloudStorageContainer = null) { if (!forumPostIds.Any()) { return(new Dictionary <Guid, IForumPostInfo>()); } var ids = forumPostIds.ToArray(); var fetchXml = XDocument.Parse(@" <fetch mapping=""logical""> <entity name=""adx_communityforumpost""> <filter type=""and""> </filter> <attribute name=""adx_date"" /> <link-entity link-type=""outer"" name=""contact"" from=""contactid"" to=""adx_authorid"" alias=""author""> <attribute name=""contactid"" /> <attribute name=""fullname"" /> <attribute name=""emailaddress1"" /> </link-entity> <link-entity link-type=""outer"" name=""annotation"" from=""objectid"" to=""adx_communityforumpostid"" alias=""attachment""> <attribute name=""annotationid"" /> <attribute name=""mimetype"" /> <attribute name=""filename"" /> <attribute name=""filesize"" /> </link-entity> <link-entity link-type=""outer"" name=""adx_communityforumthread"" from=""adx_communityforumthreadid"" to=""adx_forumthreadid"" alias=""thread""> <attribute name=""adx_communityforumthreadid"" /> </link-entity> </entity> </fetch>" ); var filter = fetchXml.Descendants("filter").First(); filter.AddFetchXmlFilterInCondition("adx_communityforumpostid", ids.Select(id => id.ToString())); var fetch = Fetch.Parse(fetchXml.ToString()); var contactLink = fetch.Entity.Links.FirstOrDefault(l => l.Name.Equals("contact")); if (contactLink != null) { contactLink.IsUnique = true; } var response = (serviceContext as IOrganizationService).RetrieveMultiple(fetch); return(ids.ToDictionary(id => id, id => { IForumPostInfo unknownInfo = new UnknownForumPostInfo(); var entities = response.Entities.Where(e => e.Id == id).ToArray(); if (!entities.Any()) { return unknownInfo; } var attachmentInfo = entities.Select(e => { var annotationId = e.GetAttributeAliasedValue <Guid?>("attachment.annotationid"); var filename = e.GetAttributeAliasedValue <string>("attachment.filename"); var mimetype = e.GetAttributeAliasedValue <string>("attachment.mimetype"); var filesize = e.GetAttributeAliasedValue <int?>("attachment.filesize").GetValueOrDefault(); if (annotationId == null || string.IsNullOrEmpty(filename)) { return null; } return new NoteAttachmentInfo(new EntityReference("annotation", annotationId.Value), filename, mimetype, filesize, websiteId, cloudStorageContainer); }).Where(info => info != null); var entity = entities.First(); var author = GetForumAuthor("author", entity); var thread = new EntityReference(("adx_communityforumthread"), entity.GetAttributeAliasedValue <Guid>("thread.adx_communityforumthreadid")); return new ForumPostInfo( new EntityReference("adx_communityforumpost", entity.Id), author, entity.GetAttributeAliasedValue <DateTime?>("adx_date").GetValueOrDefault(unknownInfo.PostedOn), attachmentInfo, thread); })); }