Read documents from SharePoint programmatically using C# (Perform a non deleting BizTalk WSS Recieve)

Posted on Updated on

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.

We can also key in specific user under the credentials of the request if desired so. Capture

Install MSI packages (Locally and on Remote computers) using WMI

Posted on Updated on

Installing of packages and msi is a day to day activity in current business scenario. And with growing use of technology and programs it becomes all the more important to have this process efficient, error free, less time consuming. Automating such process becomes all the more important.

Today we will see how we can achieve the same. WMI comes with great feature to leverage it and automate our msi installation process.

WMI namespace — root\CIMV2 has a class – “Win32_Product” which has few methods that work on msi packages. The methods being: —Admin, —Advertise, —Configure, —Reinstall, —Install, —Uninstall,–Upgrade

We will take a look at the Install method to achieve our goal. We can use WMI for automated installation either on a single machine, locally or on a group of machines.

Image

Doing a local computer Installation can be done straight forward with the package location assigned to inParams.

The key here for remote multi machine installation is the “CopyToTempLocation” method that copies the msi from the machine on which the program is running to a local location on the specified remote machine and the new path is then set to inParams.

The outParams[ReturnValue] return the code .i.e the result of execution. There is a comprehensive list of codes returned and the one we should be looking for is  Zero“0” which implies success.

Please visit http://support.microsoft.com/kb/304888 to see the list of codes returned.

 

Cross Domain WCF-SQL Adapter communication

Posted on Updated on

For cross domain WCF-SQL adapter communication between servers that communicate, the UseAmbientTransaction property should be set to false to establish the data communication.

Capture1

 

For detailed information of various WCF-SQL adapter properties please refer this.

 

BizTalk 2013 and SharePoint 2013 Integration Issue (401:Unauthorized)

Posted on Updated on

During the initial set-up of SharePoint 2013 adapter with BizTalk 2013 there may be scenario when you get errors such as  : The remote server returned an error: (401) Unauthorized.

Image

The adapter failed to transmit message going to send port “XXXX” with URL “wss://ServerXXX:XX/sites/XX/xxxxx/xxxx“. It will be retransmitted after the retry interval specified for this Send Port. Details:”[System.Net.WebException] The remote server returned an error: (401) Unauthorized.

This error was triggered by the Windows SharePoint Services receive location or send port with URI “wss://ServerXXX:XX/sites/XX/xxxxx/xxxx“.

Windows SharePoint Services adapter event ID: 12310”.

When validating you see that the user under which the adapter host is running has all the rights on the web site and the document library and still you see the issue.

Out of curiosity the user is given rights on SharePoint database and WS_WPG groups also but still the problem persists.

The problem here is because the user is not having access to the root of the SharePoint server. In this case to http://ServerXXX:XX.

When BizTalk 2013 WSS adapter tries to access the document library it first tries to access the root and then dives in to the libraries for the action. A look at the SharePoint logs will give the necessary information.

Once the user under which the host of WSS adapter is running gets access to the root site, BizTalk 2013 will start communication to and from SharePoint document libraries. The permission can be the least privileged one .i.e Visitors.

Extract details of resources contained within a BizTalk MSI package

Posted on

There are multiple ways to extract details of resources contained within a BizTalk MSI package. One being using msiexec.exe to extract the contents to a location and get the information.

However BizTalk has an excellent dll which can help get all the required resource details that comes in packed in the MSI package.

The saviour here is

Microsoft.BizTalk.ApplicationDeployment.dll that comes with BizTalk installation.

Two dlls are required to be referenced :

1. Microsoft.BizTalk.ApplicationDeployment.dll which has the method that scans for you the MSI for all its resources and then the related properties within each resource. Its placed under the root BizTalk installation folder.

2.Microsoft.BizTalk.Admin.dll from GAC

//

 Microsoft.BizTalk.ApplicationDeployment.IInstallPackage pack = null;

pack = Microsoft.BizTalk.ApplicationDeployment.DeploymentUnit.ScanPackage(“FullBizTalkMSIPath”);

// Gets all the information about all the resources compliled witin BizTalk MSI package

Microsoft.BizTalk.ApplicationDeployment.IDeploymentResource[] resources = pack.Resources;

foreach (var resource in resources  )

{    // Do something     }

Programmatically Upload and Delete documents from SharePoint library

Posted on Updated on

Documents can be uploaded or deleted from SharePoint library using C# without using SharePoint dlls.
There may arise cases when you want to performs upload and delete documents to and from SharePoint document libraries using C# but you don’t have or want to involve SharePoint dlls to perform this action.
System.Net.WebClient  and System.Net.WebRequest comes in handy in such situation.
These can be used in tandem to achieve the required goal.
 
//
// Upload document to SharePoint library
//
 
System.Net.WebClient sharePointLibrary = new System.Net.WebClient();
sharePointLibrary.Credentials = new System.Net.NetworkCredential(“SharePointUser“,”SharePointUserPassword“);
byte[] response = sharePointLibrary.UploadFile(Path.Combine(“SharePointURI“, “ResultantFileName“), “PUT“, Path.Combine(“TestLocationofFileToUpload“, “NameOfTheFileToBeUploaded“));
 
//
// Delete document from SharePoint library
//
 
var request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(“FullSharePointURIofDocument“);
request.Timeout = System.Threading.Timeout.Infinite;
request.Credentials = new System.Net.NetworkCredential(“SharePointUser“,”SharePointUserPassword“);
request.Method = “DELETE“;
var response = (System.Net.HttpWebResponse)request.GetResponse();