0

I am trying to add item to Office 365 list item, and I did use the below code:

public string PostingDeliveryNote(string ProjectName, string SupplierName, string DN_No , string PONo, 
        string HWSalesContractNo, string PackingListNo,FileStream FileName )
    {

        using (ClientContext CContext = new ClientContext(WebUrl))
        {
            CContext.Credentials = AuthinticatingUser();
            List TargetedList = CContext.Web.Lists.GetByTitle("Delivery_Note");
            ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
            Microsoft.SharePoint.Client.ListItem newItem = TargetedList.AddItem(itemCreateInfo);

            newItem["Title"] = ProjectName;
            newItem["SupplierName"] = SupplierName;
            newItem["DN No"] = DN_No;
            newItem["PONo"] = PONo;
            newItem["HWSalesContractNo"] = HWSalesContractNo;
            newItem["PackingListNo"] = PackingListNo;

            newItem.Update();
            CContext.ExecuteQuery();
        }

        return "Item added successfully";

    }

but I was not able to attach files to the list. any suggestions?

I need to extend this function to get save attachments.

the below is the updated solution for the method:

 public string PostingDeliveryNote(string ProjectName, string SupplierName, string DN_No , string PONo, 
        string HWSalesContractNo, string PackingListNo,string FileName,string FilePath)
    {

        using (ClientContext CContext = new ClientContext(WebUrl))
        {
            CContext.Credentials = AuthinticatingUser();
            List TargetedList = CContext.Web.Lists.GetByTitle("Delivery_Note");
            ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
            Microsoft.SharePoint.Client.ListItem newItem = TargetedList.AddItem(itemCreateInfo);

            newItem["Title"] = ProjectName;
            newItem["SupplierName"] = SupplierName;
            newItem["DNNo"] = DN_No;
            newItem["PONo"] = PONo;
            newItem["HWSalesContractNo"] = HWSalesContractNo;
            newItem["PackingListNo"] = PackingListNo;
            newItem.Update();
            CContext.ExecuteQuery();

            int LastID = newItem.Id;
            ///////////////////////////// Uploading the attachments
            ///

            newItem.Update();
            AttachmentCreationInformation attachmentCreateinfo = new AttachmentCreationInformation();
            attachmentCreateinfo.ContentStream = new MemoryStream(System.IO.File.ReadAllBytes(FilePath));
            attachmentCreateinfo.FileName = FileName;
            Microsoft.SharePoint.Client.Attachment att = newItem.AttachmentFiles.Add(attachmentCreateinfo);
            CContext.ExecuteQuery();

            ///////////////////////////////
        }
        return "New Item added Succfully";
    }
1

You could add attachment to list item like the below:

        newItem.Update();
        AttachmentCreationInformation attachmentCreateinfo = new AttachmentCreationInformation();
        attachmentCreateinfo.ContentStream = new MemoryStream(System.IO.File.ReadAllBytes(@"C:\test.docx"));
        attachmentCreateinfo.FileName = "test.docx";
        Attachment att = newItem.AttachmentFiles.Add(attachmentCreateinfo);
        ctx.ExecuteQuery();
    
1
  • thanks a lot dear, it was great help <3\
    – HAJJAJ
    Sep 18 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.