Easy Code to Post Messages with Attachment in Teams - Netwoven

Easy Code to Post Messages with Attachment in Teams

By Arup Kumar Maity  •  April 22, 2021  •  3375 Views

Easy Code to Post Messages with Attachment in Teams

Introduction:

Microsoft Teams is enjoying a meteoric rise for its use in recent times owing to COVID-19 and otherwise. Every custom app being developed today is jostling for its space to be in Teams. One of the items at heart would be the app’s ability to post a message with attachments into a Teams channel.

Programmatically posting attachments with messages in Teams channel can open up avenues to a lot of automated messaging options. However, an understanding of providing the attachment info within JSON structure is very important in the process. In case of an improper attachment info in JSON structure, even though the channel message will be created with the attachment, but on clicking the attachment of the channel message it will show the document and the corresponding conversations (chat messages) will not show up.

This article enumerates a stepwise process to enable the programmer to do it at ease.

Steps

Step 1:

Get access token.

Step 2:

Get document info and build the attachment JSON info.

string attachmentinfo = "{\"@type\":\"http://schema.skype.com/File\",\"version\":2,\"id\":\"" + FileId +
                                    "\",\"baseUrl\":\"" + _SiteUrl + "\"," +
                                    "\"type\":\"" + FileType + "\",\"title\":\"" + strFileName +
                                    "\",\"state\":\"" + strState + "\",\"objectUrl\":\"" + tenantUrl + FileserverRelativeUrl +
                                    "\",\"providerData\":{ \"code\":null,\"type\":0}," +
                                    "\"itemid\":\"" + FileId + "\"," +
                                    "\"fileName\":\"" + strFileName + "\",\"fileType\":\"" + FileType + "\"," +
                                    "\"fileInfo\":{ \"fileUrl\":\"" + tenantUrl + FileerverRelativeUrl +
                                    "\",\"siteUrl\":\"" + _SiteUrl + "\"," +
                                    "\"serverRelativeUrl\":\"" + FileserverRelativeUrl +
                                    "\"},\"botFileProperties\":{},\"filePreview\":{},\"fileChicletState\":{ \"serviceName\":\"teams\",\"state\":\"" + strState + "\"}}";
Step 3:

Post channel message with attachment.

string strURL = String.Format("https://apac.ng.msg.teams.microsoft.com/v1/users/ME/conversations/{0}/messages", HttpUtility.UrlEncode(ChannelId));
                var httpWebRequest = (HttpWebRequest)WebRequest.Create(strURL);
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Headers.Add("RegistrationToken:" + strRegistrationtoken);
                httpWebRequest.Method = "POST";

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                { 
string json = "";
json = "{\"content\":\"" + Postmsg + "\",\"messagetype\":\"RichText/Html\",\"contenttype\":\"text\",\"amsreferences\":[],\"clientmessageid\":\"" + clientmessageid + "\"," +
                                      "\"imdisplayname\":\"svc_AZ_shpmigration1\"," +
                                      "\"properties\":{\"files\":[" + attachmentinfo + "],"
                                      + "\"importance\":\"\"}}";

 streamWriter.Write(json);
                    streamWriter.Flush();
                    streamWriter.Close();
                }

var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                    using (WebResponse resp = httpWebRequest.GetResponse())
                    {
                        ReplyMessageid = resp.Headers["Location"];
                        ReplyMessageid = ReplyMessageid.Substring(ReplyMessageid.LastIndexOf("/") + 1);
                        return ReplyMessageid;
                    }

So, where should we use this artefact? While you are embarking on any custom development integrating with MS Teams, an attachment can accompany a message anytime. You may be interacting with a BOT, posting a message from a workflow, reposting messages from any other chat rooms to Teams channels and there is no end to it. This simple artefact can come in handy and you can build on it easily. Let me know if it does!

Stay tuned for more posts for custom programming snippets that you can readily use while creating apps for Teams.

Leave a comment

Your email address will not be published. Required fields are marked *

Dublin Chamber of Commerce
Microsoft Partner
Microsoft Partner
Microsoft Partner
Microsoft Partner
Microsoft Partner
Microsoft Partner
Microsoft Fast Track
Microsoft Partner
Microsoft Fabric
MISA
MISA
Unravel The Complex
Stay Connected

Subscribe and receive the latest insights

Netwoven Inc. - Microsoft Solutions Partner

Get involved by tagging Netwoven experiences using our official hashtag #UnravelTheComplex