Skip to content
This repository has been archived by the owner on Oct 4, 2021. It is now read-only.

using CreateAttachmentAsync i created task by passing memory stream and created work item then the attached file is showing 0KB ? is there any other way to add attachment? i have debugged memory stream contains data. please help any one #316

Open
kolimihariprasad opened this issue Sep 29, 2021 · 0 comments

Comments

@kolimihariprasad
Copy link

image

my code:
public async Task CreateTask(string user,string id,string contentType, string Param, string Taskdesc)
{
try
{
ContentPlanningDAL contentPlanningDAL = new ContentPlanningDAL(_config);
DataTable dt = new DataTable();
dt = contentPlanningDAL.GetContentDetaisByKeywordID(id);
Uri uri = new Uri(_uri);
string personalAccessToken = contentPlanningDAL.GetUserPAT(user);
string project = _project;

            VssBasicCredential credentials = new VssBasicCredential("", personalAccessToken);
            VssConnection connection = new VssConnection(uri, credentials);
            WorkItemTrackingHttpClient workItemTrackingHttpClient = connection.GetClient<WorkItemTrackingHttpClient>();

            AttachmentReference attachment;

            CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(AccessKey);
            CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
            string strContainerName = ContainerName;
            CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(strContainerName);
            string[] url = Param.Split('/');
            string fileUrl = HttpUtility.UrlDecode(url[4]);
            fileUrl = fileUrl.Replace("#", "");
            var blob = cloudBlobContainer.GetBlockBlobReference(fileUrl);
            string fileName = HttpUtility.UrlDecode(url[4].Split('_')[0]);

            Stream blobStream = await blob.OpenReadAsync();
            FileStreamResult fileStreamResult = File(blobStream, blob.Properties.ContentType);

            MemoryStream ms = new MemoryStream();
           // MemoryStream mst = new MemoryStream();

            blobStream.CopyTo(ms);
            
            attachment = workItemTrackingHttpClient.CreateAttachmentAsync(ms, fileUrl,"Simple", null, null, default).Result;
            

            JsonPatchDocument patchDocument = new JsonPatchDocument();

            patchDocument.Add(
                new JsonPatchOperation()
                {
                    From = null,
                    Operation = Operation.Add,
                    Path = "/relations/-",
                    Value = new
                    {
                        rel = "AttachedFile",

                        url = attachment.Url,
                        attributes = new { comment ="",DateAttached=DateTime.Now  }
                }}
            );

            //task title
            patchDocument.Add(
                new JsonPatchOperation()
                {
                    From = null,
                    Operation = Operation.Add,
                    Path = "/fields/System.Title",
                    Value = dt.Rows[0].Field<string>("Title")
                }
            );

            patchDocument.Add(
                    new JsonPatchOperation()
                    {
                        From = null,
                        Operation = Operation.Add,
                        Path = "/fields/System.CreatedDate",
                        Value = DateTime.Now
                    }
                );
            patchDocument.Add(
                    new JsonPatchOperation()
                    {
                        From = null,
                        Operation = Operation.Add,
                        Path = "/fields/System.CreatedBy",
                        Value = user
                    }
                );
            //activity
            patchDocument.Add(
               new JsonPatchOperation()
               {
                   From = null,
                   Operation = Operation.Add,
                   Path = "/fields/Microsoft.VSTS.Common.Activity",
                   Value = "Development"
               });
            
            string desc = dt.Rows[0].Field<string>("Description");
            if(desc != null)
            {
                patchDocument.Add(
             new JsonPatchOperation()
             {
                 From = null,
                 Operation = Operation.Add,
                 Path = "/fields/System.Description",
                 Value = desc
             });
            }
            
            patchDocument.Add(
           new JsonPatchOperation()
           {
               From = null,
               Operation = Operation.Add,
               Path = "/fields/System.AssignedTo",
               //for value both email id and name can be passed
               //Value = dt.Rows[0].Field<string>("AssignedTo")
               Value = "[email protected]"
           });
            

            //tags
            patchDocument.Add(
         new JsonPatchOperation()
         {
             From = null,
             Operation = Operation.Add,
             Path = "/fields/System.Tags",
             Value = "Tags"
         });

            
            //type of work item
            string wiType = "Task";//Console.ReadLine();

            var result = workItemTrackingHttpClient.CreateWorkItemAsync(patchDocument, project, wiType).Result;
            //WorkItem result = workItemTrackingHttpClient.CreateWorkItemAsync(patchDocument, project, wiType).Result;
            FileInfo fileInfo = new FileInfo(path);
            //if (fileInfo.Exists)
            //{
            //    fileInfo.Delete();
            //}
            int taskId = Convert.ToInt32(result.Id);
            if(taskId > 0)
            {
                //int uploadedRows = contentPlanningDAL.UpdateTaskWithTicketId(taskId, id);
            }
            return result;
        }
        catch (Exception ex)
        {
            ExceptionLoggingController exceptionLoggingController = new ExceptionLoggingController(_config);
            exceptionLoggingController.InsertError(ex.Message, "create task for azue bord", "API", "", ex.GetHashCode().ToString(), ex.StackTrace, "", "", "", "");

        }
        finally{
            
        }

        return null;
    }
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant