GetItem operation - how to access the byte stream?

Feb 24, 2012 at 5:33 PM

I've been toying around with using the GetItem operation to retrieve an item from a document library.  I can successfully call the operation, but I can't seem to populate a variable with the resulting byte stream.  Any pointers on how to do this?

Feb 24, 2012 at 7:55 PM
Edited Feb 24, 2012 at 8:00 PM

Seek and you shall find your own answer:

The xData.reponseXML object has a slightly different structure for this call:

 

<soap:envelope>
   <soap:body>
      <getitemresponse>
          <getitemresult>0</getitemresult>
          <fields>_</fields> 
          <stream>_</stream>
      </getitemreponse>
    </soap:body>
</soap:envelope>

The <fields> and <stream> elements contain the data described in the MSDN documentation.  Stream contains the base64 encoded binary data.

I had a lot of trouble selecting the desired node from the xData object... a namespace issue perhaps?  If it helps anyone, here is the call I used (link contains the absolute path to the item, e.g. http://server/site/library/item.doc):

$().SPServices({
	operation: "GetItem",
	async: false,
	Url: link,
	completefunc: function(xData, Status){
		// out is the binary version of the file
		var out = xData.responseXML.documentElement.childNodes[0].childNodes[0].childNodes[2].textContent;
	}
});

 

 

May 3, 2012 at 6:27 AM

I have tried the code above but I'm getting "error" on Status. I'm trying to get data from an Infopath .XML document. Any ideas?

May 3, 2012 at 2:12 PM

Please paste the code you used when you got Status == "error"...

If the variable 'Status' is the one giving you an error, this means the request failed to reach the SP server... (Status is the start of the ajax request... not a status from SP's processing your request message)...  I suspect it is something your code is not setting correctly.

Paul

May 3, 2012 at 2:43 PM

Here is the code I used. And the error was on the 'Status' variable.

$().SPServices({
	operation: "GetItem",
	async: false,
	Url: 'http://server/site/library/F1196C2F-B994-E111-8B74-0050568404B3.xml',
	completefunc: function(xData, Status){
		// out is the binary version of the file
                alert(Status);
		var out = xData.responseXML.documentElement.childNodes[0].childNodes[0].childNodes[2].textContent;
	}
});

May 3, 2012 at 3:21 PM

Just tried a quick example to retrieve an XML file and I was able to successful retrieve it using your example above...

Is your URL valid? and on the same site from where this code is being run (you can't go across different url domains)? Can you take your URL, and paste it into a broswer URL address bar and validate that it you can access it?

Paul

May 3, 2012 at 3:31 PM

Yeah, sorry.. I forgot this line:  webURL: 'http://server/site/library/',

I'm on the same domain but not running this on the server side. I have it working now. I'm getting the base64 data now. Any pointers on how to decode using JQuery?

Thanks for all your help!

May 3, 2012 at 3:33 PM

Ok... good... (my test was browser side as well, using jQuery/SPSservices)

take a look at teh forums... in the last week, I remember seeing a thread that pointed to a jQuery library able to encode and decode base64 streams... Good luck.

Paul.

May 3, 2012 at 4:19 PM

Sorry to keep bugging but I've got it decoded now but it's all text. Is there any way to get the complete XML structure of the document?

Thanks!

May 3, 2012 at 4:22 PM

What do you mean it is all text?  Did it strip out the xml tags? Or are you now looking to load the "text xml" into a Document Tree?

If after decoding you do have valid xml, then the following should get you back a jquery object of that xml that you can use to filter, search, manipulate:

var $xmDoc = $('your xml text here')

Paul.

May 3, 2012 at 4:27 PM

I mean the decode process stripped all the xml tags. I guess the issue is with the decode process. 

May 3, 2012 at 4:35 PM

Sounds like that... unless SP remove them before sending the response... You should have enough to figure that piece out...

Good Luck.

paul

May 3, 2012 at 6:03 PM

Ok. So SP is not removing the xml tags. The issue is the XML file has file attachments that are base64 also. That is really messing up the decoding. There has to be a better way of reading the file contents. I'd also like to be able to edit the file contents. Is any of this possible? 

I saw another post from you saying: "I have not run across the need to update a file's content (yet)." - Well here it is.... :)

May 3, 2012 at 6:18 PM

:)

This may actually be the point where I go silent and just watch... 

May 4, 2012 at 12:03 PM

What type of XML structure do the files have? What are they used for and why again do you need to manipulate them? Also, what browser does this need to work in. I have a thought or two for IE. Though, if you are using a document library, why would you have a document that also had attachments? Just curious. There may be a way to just get the file without attachments but I have never tried it!