0

as per my requirement I created a PDF using iTextsharp and i want upload it to a specific document set (dynamic) in a document library when an event receiver is triggered from a list.

Can anyone please help me with this ?

  • Do you want idea to implement it or are you facing any issues in your code ? – Karthik Jaganathan Nov 18 '19 at 9:22
  • I just want to know how to get the URL of a document set which is present in a document library, not the relative url the absolute url so that i can add the byte file ex: SPFile f = mylibrary.Files.add("server/sitecol/doclib/docset/file.pdf",bytes,true) I just to get the URL – Samrat Guha Nov 19 '19 at 8:00
0

My sample test code for your reference.

public override void ItemAdded(SPItemEventProperties properties)
        {
            base.ItemAdded(properties);
            using (SPWeb web = properties.Web)
            {
                using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
                {
                    Document document = new Document(PageSize.A4, 10, 10, 10, 10);

                    PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
                    document.Open();


                    Paragraph para = new Paragraph(properties.ListItem["Title"].ToString());

                    document.Add(para);

                    string text = @"you are successfully created pdf file.";
                    Paragraph paragraph = new Paragraph();
                    paragraph.SpacingBefore = 10;
                    paragraph.SpacingAfter = 10;
                    paragraph.Alignment = Element.ALIGN_LEFT;
                    paragraph.Font = FontFactory.GetFont(FontFactory.HELVETICA, 12f, BaseColor.GREEN);
                    paragraph.Add(text);
                    document.Add(paragraph);

                    document.Close();
                    byte[] bytes = memoryStream.ToArray();

                    string libraryName = "doc";
                    SPList lib = web.Lists.TryGetList(libraryName);
                    SPFolder folder = web.GetFolder(lib.RootFolder.ServerRelativeUrl + "/testDocumentSet");

                    string dsturl = folder.ServerRelativeUrl + "/test.pdf";
                    SPFile myfile = folder.Files.Add(dsturl, bytes, true);                    
                    lib.Update();                    
                    memoryStream.Close();
                }
            }
        }

Update:

Don't use encoded url in c# code.

I tested in my local SharePoint 2013 by creating a document set called [test, aaads], the code works.

SPFolder folder = web.GetFolder(lib.RootFolder.ServerRelativeUrl + "/test, aaads");
string fullurl= web.Url+folder.Url + "/test.pdf";

Debug Screenshot:

enter image description here

| improve this answer | |
  • The document set is having a comma and space Like Guha, Samrat and the url looks like '..../Guha%2C%20Samrat' is there any way to solve this ?? Or else I have to create a string and genrate the asked format. I also require to send an email so that the user gets direct access to the pdf I created. – Samrat Guha Nov 19 '19 at 13:14
  • Check my update. – Lee_MSFT Nov 20 '19 at 8:18

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.