Read documents from SharePoint programmatically using C# (Perform a non deleting BizTalk WSS Recieve)
Documents from SharePoint can be read programmatically using C# WebRequest.
On a BizTalk front, such need occurs when you want to perform a non deleting receive for a sharepoint document.
Consider a scenario where your business process gets triggered and then within needs to read data from sharepoint document library and use it in the process.
BizTalk by default will delete the document from the library after reading the same. One can argue that we can implement a process within, where in the BizTalk process will read the document and then put it back to the location through a WSS send port and we are done. This is absolutely fine and works as expected but as you see it has more over heads and more failure point. What if while uploading the document it fails and the nest process never gets to read the data and hence resulting in subsequent failures of the main business process. And above all you still need to have a C# code to activate , deactivate your receive location.
What comes in handy in such business scenario is that we can have the Sharepoint location read from a config, preferably SSO Config and then use C# code to read the data to stream or memory stream and then use the same within our business process.
Using memorystream would also help load it to BizTalk XLAng message. Also we can implement a full BizTalk streaming mechanism here to preform our action using IStreamfactory. More details on this can be found here .
Using the below code we can read the contents to a stream and then to memorystream. UseDefaultCredentials will run the program under current user account or in BizTalk Orch case, under the BizTalk host instance account. So make sure that the user has sufficient rights to read the document library.