private void ResetContext(string url) { _provider.SiteUrl = url; var context = _provider.Create(); _contentTypeFactory = new ContentTypeFactory(context); _listFactory = new ListFactory(context); _service = new SharepointService(context); }
private void MoveAttachmentsToSharepoint() { if (IsMessage(PluginMessage.Create, PluginMessage.Update) && IsStage(PluginStage.PostEvent) && IsMode(PluginMode.Asynchronous)) { if (!GetBoolean(Fields.email_.directioncode) && FieldChanging(Fields.email_.regardingobjectid)) { if (GetLookupType(Fields.email_.regardingobjectid) == Entities.incident) { if (IsMessage(PluginMessage.Create)) { //lets just snooze for 10 seconds if this is a create message //in case attachments are added after the initial create message Thread.Sleep(10000); } var documentExtentions = new[] { "pdf", "doc", "docx" }; var attachmentQuery = XrmService.BuildQuery(Entities.activitymimeattachment, null, new[] { new ConditionExpression(Fields.activitymimeattachment_.activityid, ConditionOperator.Equal, TargetId) }); var typeFilter = attachmentQuery.Criteria.AddFilter(LogicalOperator.Or); typeFilter.Conditions.AddRange(documentExtentions.Select(de => new ConditionExpression(Fields.activitymimeattachment_.filename, ConditionOperator.EndsWith, de))); var attachmentsToMove = XrmService.RetrieveAll(attachmentQuery); if (attachmentsToMove.Any()) { var regardingObjectId = GetLookupGuid(Fields.email_.regardingobjectid); var regardingType = GetLookupType(Fields.email_.regardingobjectid); var regardingName = XrmEntity.GetLookupName(XrmService.LookupField(TargetType, TargetId, Fields.email_.regardingobjectid)); //get a sharepoint folder for the target location record var documentFolder = SharepointService.GetOrCreateDocumentFolderRelativeUrl(regardingType, regardingObjectId.Value, regardingName); foreach (var item in attachmentsToMove) { var fileName = item.GetStringField(Fields.activitymimeattachment_.filename); //if a txt file could potentially have already been moved so lets just leave txt files in crm if (fileName != null && !fileName.EndsWith(".txt")) { var fileContent = item.GetStringField(Fields.activitymimeattachment_.body); //load the attachment to sharepoint var documentUrl = SharepointService.UploadDocument(documentFolder, fileName, fileContent, "[EmailFile]", item.Id.ToString().ToUpper().Replace("{", "").Replace("}", "").Replace("-", "")); ////update the attachment to a text file with the sharepoint url //item.SetField(Fields.activitymimeattachment_.filename, fileName + ".txt"); //item.SetField(Fields.activitymimeattachment_.body, Convert.ToBase64String(("Moved to sharepoint - " + documentUrl).ToByteArray())); //XrmService.Update(item, new[] { Fields.activitymimeattachment_.filename, Fields.activitymimeattachment_.body }); } } } } } } }
private async Task OnFormComplete(IDialogContext context, IAwaitable <SharepointModel> awaitableResult) { SharepointModel spModel = await awaitableResult; var service = new SharepointService(); spModel.Owner = await service.GetOwnerNameForSite(spModel.Token, spModel.SiteId); context.PrivateConversationData.SetValue("spModel", spModel); PromptDialog.Confirm( context, SpAction, $"The owner of the site is {spModel.Owner.Name}. Do you want me to send an email to the owner or create a ticket?", options: new [] { "Email", "Ticket" }, patterns: new[] { new[] { "Email" }, new[] { "Ticket" } } ); }
public void EmailPluginTestMoveDocumentsToSharepointWhenRegardingCase() { var fileNames = new[] { "Sample Attachment.PNG", "Sample Attachment.PDF", "Sample Attachment.DOCX" }; var documentContent = Convert.ToBase64String(File.ReadAllBytes("Test Document.txt")); //okay lets create a case, then an email regarding it with attachments and verify //the ones with our valid extensions are created in sharepoint var aCase = CreateTestRecord(Entities.incident, new Dictionary <string, object> { { Fields.incident_.customerid, TestContact.ToEntityReference() } }); var email = CreateTestRecord(Entities.email, new Dictionary <string, object>() { { Fields.email_.regardingobjectid, aCase.ToEntityReference() }, { Fields.email_.subject, "TESTATTACHMENTSCRIPT" }, { Fields.email_.directioncode, false } }); foreach (var filename in fileNames) { CreateTestRecord(Entities.activitymimeattachment, new Dictionary <string, object>() { { Fields.activitymimeattachment_.activityid, email.ToEntityReference() }, { Fields.activitymimeattachment_.filename, filename }, { Fields.activitymimeattachment_.body, documentContent } }); } WaitTillTrue(() => { return(SharepointService.GetDocumentFolderRelativeUrl(aCase.LogicalName, aCase.Id) != null); }, 60); var sharePointFolder = SharepointService.GetDocumentFolderRelativeUrl(aCase.LogicalName, aCase.Id); WaitTillTrue(() => { return(SharepointService.GetDocuments(sharePointFolder).Count() == 2); }, 60); var sharePointDocuments = SharepointService.GetDocuments(sharePointFolder); Assert.IsTrue(sharePointDocuments.Count(sd => sd.Name.EndsWith(".PDF")) == 1); Assert.IsTrue(sharePointDocuments.Count(sd => sd.Name.EndsWith(".DOCX")) == 1); //okay lets do the similar scenario but where an existing received email is set regarding the case //this is actually the scenario when created by case creation rules email = CreateTestRecord(Entities.email, new Dictionary <string, object>() { { Fields.email_.subject, "TESTATTACHMENTSCRIPT" }, { Fields.email_.directioncode, false } }); foreach (var filename in fileNames) { CreateTestRecord(Entities.activitymimeattachment, new Dictionary <string, object>() { { Fields.activitymimeattachment_.activityid, email.ToEntityReference() }, { Fields.activitymimeattachment_.filename, filename }, { Fields.activitymimeattachment_.body, documentContent } }); } email.SetLookupField(Fields.email_.regardingobjectid, aCase); email = UpdateFieldsAndRetreive(email, Fields.email_.regardingobjectid); WaitTillTrue(() => { return(SharepointService.GetDocumentFolderRelativeUrl(aCase.LogicalName, aCase.Id) != null); }, 60); sharePointFolder = SharepointService.GetDocumentFolderRelativeUrl(aCase.LogicalName, aCase.Id); //this time we should have twice as many attachments for each thing //because the first email already added some WaitTillTrue(() => { return(SharepointService.GetDocuments(sharePointFolder).Count() == 4); }, 60); sharePointDocuments = SharepointService.GetDocuments(sharePointFolder); Assert.IsTrue(sharePointDocuments.Count(sd => sd.Name.EndsWith(".PDF")) == 2); Assert.IsTrue(sharePointDocuments.Count(sd => sd.Name.EndsWith(".DOCX")) == 2); DeleteMyToday(); }
private IForm <SharepointModel> BuildForm() { return(new FormBuilder <SharepointModel>() .Field(new FieldReflector <SharepointModel>(nameof(SharepointModel.SitenameOrUrl)) .SetValidate(async(state, value) => { if (string.Equals((string)value, "None", StringComparison.Ordinal)) { return new ValidateResult { IsValid = false, Feedback = "Sorry I couldn't find the site based on your input." }; } string siteNameOrUrl = value.ToString(); bool isUri = Uri.TryCreate(siteNameOrUrl, UriKind.Absolute, out Uri uri) && (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps); var service = new SharepointService(); if (isUri) { var response = await service.ValidateSite(state.Token, uri); if (response.IsValid && response.MatchingUris.Count == 1) { state.Url = uri; state.SiteId = response.SiteId; return new ValidateResult { IsValid = response.IsValid, Value = value }; } else { return new ValidateResult { IsValid = false, Feedback = "The site url is not valid." }; } } else { var response = await service.ValidateSite(state.Token, value.ToString(), state.ChoicesGiven); if (response.IsValid && response.MatchingUris.Count == 1) { state.Url = response.MatchingUris.First(); state.Sitename = siteNameOrUrl; state.SiteId = response.SiteId; return new ValidateResult() { IsValid = response.IsValid, Value = value }; } else if (response.IsValid) { state.ChoicesGiven = true; var x = new ValidateResult() { IsValid = false, Choices = response.MatchingSites.Select(u => new Choice() { Value = u, Description = new DescribeAttribute(u), Terms = new TermsAttribute(u) }).Union(new[] { new Choice() { Value = "None", Description = new DescribeAttribute("None"), Terms = new TermsAttribute("None") } }), Value = value, Feedback = "Here are some sites I found." }; return x; } else { return new ValidateResult() { IsValid = response.IsValid, Feedback = "The name of site is not valid." }; } } })) .Field(nameof(SharepointModel.AccessRights)) .Confirm(async spModel => new PromptAttribute($"Do you confirm that this is the site you would like {spModel.AccessRights} access for: {spModel.Url.ToString()}?")) .Build()); }