I am trying to create items in my Sitecore Media libary via Sitecore Web API. Our version of Sitecore is 8.2. My main concern is if I am formulating the request properly and if there is a place like my my "showConfig.aspx" that can give me instruction guidance. Here is my code ...

Console app: Main


Here is the PostMediaItem:

 public static void PostMediaItem()
            // Potentially could be used in our console app

            string HOST = @"https://intranet-dev.university.edu";
            string ParentID = "{2260D52E-74AA-46D4-864B-701C82D49B6D}";         //Must be media library folder/item. this is the root
            string mediaFileExt = ".pdf";                                       //you need to specify the extension. You can get that by the content-type returned or the way you find better.

            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();

            //create a instance and inform the credentials as well the host
            MediaPost mp = new MediaPost(HOST, @"sitecore\ad\UserName", "Password1");

            //post the media!
            //Important: It needs a Valid Item Name,
            //                    a Valid Parent ID under Media Library,
            //                    the database, the stream and the extension
            mp.PostMedia("Class List", new Guid(ParentID), "master", mediaStream, mediaFileExt);


Class: MediaPost

 public class MediaPost
        public string Host { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
        public MediaPost(string host, string username, string password)
            this.Host = host;
            this.Username = username;
            this.Password = password;
        public void PostMedia(string itemName, Guid parentId, string databaseName, Stream fileStream, string fileExtension)
            //create the url
            string url = String.Format("{0}/-/item/v1/?", this.Host.TrimEnd('/'));
            //append parameters
            url += String.Format("name={0}&sc_itemid={1}&sc_database={2}&payload=content"
              , HttpUtility.UrlEncode(itemName)
              , HttpUtility.UrlEncode(parentId.ToString())
              , HttpUtility.UrlEncode(databaseName));
            //create request instance
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.KeepAlive = false;
            request.Headers.Add("X-Scitemwebapi-Username", this.Username);
            request.Headers.Add("X-Scitemwebapi-Password", this.Password);
            //apply content type and boundary
            string boundary = "---------------------------" + DateTime.Now.ToString("yyyyMMddHHmmssfff", System.Globalization.CultureInfo.InvariantCulture);
            byte[] boundaryBytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
            request.ContentType = "multipart/form-data; boundary=" + boundary;
            using (Stream stream = request.GetRequestStream())
                stream.Write(boundaryBytes, 0, boundaryBytes.Length);
                //file header
                string header = "Content-Disposition: form-data; name=\"file\"; filename=\"" + itemName + fileExtension + "\"\r\nContent-Type: multipart/form-data\r\n\r\n";
                byte[] bytes = System.Text.Encoding.UTF8.GetBytes(header);
                stream.Write(bytes, 0, bytes.Length);
                //file bytes
                byte[] buffer = new byte[32768];
                int bytesRead;
                if (stream != null)
                    // upload from a given stream
                    while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                        stream.Write(buffer, 0, bytesRead);
                byte[] end = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
                stream.Write(end, 0, end.Length);
            //send post

When I run it, this is what the URL looks like:


Our Intranet address is:


and I keep getting the error:

enter image description here

What steps can I take to troubleshoot this?


1 Answer 1


Instead of using previous Sitecore Item Web Api, use the latest ItemService REST API which supports Sitecore versions 7.5+. More information about the ItemService can be found at Developer's Guide to Sitecore.Services.Client.

The whole documentation on the Developer's Guide is based on Javascript code snippet and based on your question, you are using a console C# app to create item. Below is a code snippet example on how to implement creation of item in C#.

Model for Authentication

public class Authentication
    public string Domain { get; set; }

    public string Username { get; set; }

    public string Password { get; set; }

Model for Item to create

public class ItemRequest
    public string ItemName { get; set; }

    public string TemplateID { get; set; }

    public string Title { get; set; }

    public string Text { get; set; }

Console Main Implementation

public static void Main(string[] args)
        var authUrl = "https://<domain>/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;

        var authResponse = authRequest.GetResponse();

        Console.WriteLine($"Login Status:\n\r{((HttpWebResponse)authResponse).StatusDescription}");


        var emailObj = new ItemRequest
            ItemName = "Test APi 4",
            TemplateID = "{76036F5E-CBCE-46D1-AF0A-4143F9B557AA}",
            Title = "Test API TITLE",
            Text = "TEST API TEXT"

        var url = "https://<domain>/sitecore/api/ssc/item/sitecore%2Fcontent%2Fhome";

        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();

        Console.WriteLine($"Item Status:\n\r{((HttpWebResponse)response).StatusDescription}");
    catch (Exception ex)
        Console.WriteLine($"Error occurred. Message: {ex.Message}.\r\n StackTrace: {ex.StackTrace}.\r\n InnerException: {ex.InnerException}");



enter image description here


  1. A cookie container is created so that after the authentication, the next requests can validate whether the request can create the item or returns a 403 Forbidden Status .

  2. I had to update the setting Sitecore.Services.SecurityPolicy to set it as Sitecore.Services.Infrastructure.Web.Http.Security.ServicesOnPolicy, Sitecore.Services.Infrastructure. The ServicesOnPolicy allows access to all Entity and Item Services.

  3. Authentication URL should be on HTTPS.

  • So, I did use your code snippet from above, I read the documentation in the guide "Sitecore.Services.Client". It seems I am almost there, I did update the Sitecore.Services.SecurityPolicy to ServicesOnPolicy but I am still receiving an Internal Services Error 500. I checked the logs, but I didn't find anything use there. Jun 8, 2020 at 23:36
  • If you are getting error 500 it may be that it is creating an item with same name twice Jun 9, 2020 at 4:49
  • i will continue to focus on this again today. It isn't the item it is trying to create because it is failing on the getResponse() line. I will try to figure this out. thanks Jun 9, 2020 at 11:42
  • Is it the first getResponse that is failing? Jun 9, 2020 at 11:44
  • yes, the first getRepsonse is failing, that is where I am trying to focus right now. looking at the exception, no inner exception, and nothing that is standing out as an error. I do know my user id and password works. thanks for all of your help Jun 9, 2020 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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