1

I am using the following to Create an item, and it works to create a folder in the media library, but I want to create/upload a pdf using this or a similar technique. Here is my code:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;
    var authUrl = "https://mysitecoresite.com/sitecore/api/ssc/auth/login";
    var authData = new Authentication
    {
        Domain = "sitecore",
        Username = "admin",
        Password = "b"
    };

    var authRequest = (HttpWebRequest)WebRequest.Create(authUrl);

    authRequest.Method = "POST";
    authRequest.ContentType = "application/json";

    var requestAuthBody = JsonConvert.SerializeObject(authData);

    var authDatas = new UTF8Encoding().GetBytes(requestAuthBody);

    using (var dataStream = authRequest.GetRequestStream())
    {
        dataStream.Write(authDatas, 0, authDatas.Length);
    }
    CookieContainer cookies = new CookieContainer();

    authRequest.CookieContainer = cookies;
    // System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
    var authResponse = authRequest.GetResponse();
    
    Console.WriteLine($"Login Status:\n\r{((HttpWebResponse)authResponse).StatusDescription}");


    authResponse.Close();

    HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create("http://sdn.sitecore.net/upload/sdn5/modules/sitecore%20item%20web%20api/sitecore_item_web_api_developer_guide_sc65-66-usletter.pdf");
    HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse();
    
    Stream mediaStream = httpWebReponse.GetResponseStream();
    string itemName = "Test PDF";
    string mediaFileExt = ".pdf";

    var emailObj = new ItemRequest
    {
        ItemName = "PtrTest2",
        TemplateID = "{FE5DD826-48C6-436D-B87A-7C4210C7413B}",
        Title = "Class List Test June",
        Text = "Class List Test June 2020",
        Media = mediaStream
            

    };
    var url = "https://mysitecoresite.com/sitecore/api/ssc/item/sitecore%2FMedia%20library";

    var request = (HttpWebRequest)WebRequest.Create(url);

    request.Method = "POST";
    request.ContentType = "application/json";
    request.CookieContainer = cookies;

    var requestBody = JsonConvert.SerializeObject(emailObj);

    var data = new UTF8Encoding().GetBytes(requestBody);

    using (var dataStream = request.GetRequestStream())
    {
        dataStream.Write(data, 0, data.Length);
    }

    // var response = request.GetResponse();

    using (WebResponse response = request.GetResponse())
    {
        Console.WriteLine($"Item Status:\n\r{((HttpWebResponse)response).StatusDescription}");
    }    
}
catch (WebException ex)
{
        Console.WriteLine($"Error occurred. Message: {ex.Message}.\r\n StackTrace: {ex.StackTrace}.\r\n InnerException: {ex.InnerException}");
        Console.WriteLine(new StreamReader(ex.Response.GetResponseStream())
                .ReadToEnd());
}

Console.ReadKey();
        

Note: I really just need to know what field the actual PDF body should be mapped to.

3
  • Hi Paul, follow this article, in this article you can see how to create media item programmatically so that from this you can take some help - stackoverflow.com/questions/30732440/… Jul 10 '20 at 8:03
  • Thank you, I will review. I know I am just about there. Best Regards, Jul 10 '20 at 11:52
  • Have you considered using SPE remoting? Would allow you to upload through PowerShell. Oct 10 '20 at 23:24
1

Note: I really just need to know what field the actual PDF body should be mapped to.

The actual field name that holds the file data is the Blob field. The type of this field is Attachment. The name Media is just the field title or similar to display name.

The template where this field resides can be found at /sitecore/templates/System/Media/Unversioned/File.

5
  • Thank you so much!! I saw that field ... this is so helpful. I have been trying to figure this out for a while. I will let you know. Jul 10 '20 at 11:52
  • @PaulT.Rykiel, as per the question you posted, did my answer help? If so, please mark it as answer. Thanks! Jul 16 '20 at 14:51
  • I haven't worked on this yet. I am hopeful that this will resolve my issue and once I have confirmed this, I will be certain to mark the answer. I always do and I always appreciate the advice. Jul 16 '20 at 21:52
  • Nice.. Thanks ! Jul 17 '20 at 6:16
  • Well, I am still having issues trying to figure how to link the pdf. Sitecore Blob brings up Azure. Jul 17 '20 at 20:34

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.