0
votes

We are using Sitecore 6.5 in the project.

In one of the sites of the project the user has to be able to upload pdf / doc files to the server. When he uploads this is getting added to the web db so we have to transfer it to the master db.

I'm trying to accomplish this by using the item web api of Sitecore.

I have looked at this post on how to do it. But I can't get it working.

Whenever I execute my request, I get an internal server error telling me that the layout for the requested document was not found.

The generated url I have is this:

{host}/-/item/v1/?name=45c87793-4b37-40c8-acbc-a3b8f2b0c275&sc_itemid=653a9a07-6ea0-4e9a-8214-6bf3447096ed&sc_database=master&payload=min

As far as I know, I think the url is built up correct.

Executing a get request does work by using below url:

{host}/-/item/v1/?sc_itemid=653a9a07-6ea0-4e9a-8214-6bf3447096ed

Any ideas on what I'm missing?

Edit:

Here's the code on how I call the media poster.

var mediaPoster = new MediaPoster("http://{host}/", @"sitecore\admin", "b");

if (pdfStream != null)
     mediaPoster.PostMedia(item.Name, new Guid("{653A9A07-6EA0-4E9A-8214-6BF3447096ED}"), "master", new MemoryStream(pdfStream.ToArray()), ".pdf");
1
Hi, can you add the code where you call mp.PostMedia? What is the extension of this file. I see you used a guid as the file name, is that what you intend? - RobertoBr
Hi Roberto, I've added the code where I call the PostMedia function. The extension as you can see in the code sample is a pdf. And it is my intent to use a guid as file name. - JeremyVm

1 Answers

0
votes

also verify your configuration is correct. To upload files you need to set security.

  <site name="website">
    <patch:attribute name="itemwebapi.mode">StandardSecurity</patch:attribute>
    <patch:attribute name="itemwebapi.access">ReadWrite</patch:attribute>
    <patch:attribute name="itemwebapi.allowanonymousaccess">false</patch:attribute>
  </site>

I just ran a test with that configuration in place and it worked.

You can create a console application and with the PostMedia class use this code:

string HOST = @"http://HOST/";
string ParentID = "{3D6658D8-A0BF-4E75-B3E2-D050FABCF4E1}";         //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\admin", "b");

//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("DocumentName", new Guid(ParentID), "master", mediaStream, mediaFileExt);

It should work, let know if you still getting that error...

UPDATE:

Do you know the showconfig.aspx in Sitecore? You can use it to see how your configuration files got parsed. I'd suggest to you to go there http://HOST/sitecore/admin/showconfig.aspx, search for <sites> and see how your website is set. All the patch attributes(mode, access, allowanonumousaccess) should be like in the configuration above.

As long as your user is admin it should have access to the medialibrary and all other security aspects.

Another point I just thought. Did you get the right version of the Item Web Api module for your Sitecore version.

1.0

The Sitecore Item Web API module v1.0.0 rev.120828 is released.
The Sitecore Item Web API module has been tested with Sitecore CMS 6.5.0 rev.120706 (Update-5) and CMS 6.6.0 rev.120918 (Initial release).

1.2

The Sitecore Item Web API 1.2.0 rev. 140128 is released.
The Sitecore Item Web API module has been tested with Sitecore CMS 6.6.0 rev. 131211 (6.6.0 Update-7) and 7.1 rev. 140130 (7.1 Update-1).