I had this requirement to create folders in sharepoint through a crm 2013 plugin. I used webservice approach for this.
However, this worked only for CRM On-premise and Sharepoint On-Premise / Sharepoint Online.

It didn’t work for me when I tried to use it from CRM-Online towards Sharepoint Online.

Reason being, Sharepoint Online authentication has a dependency to include Sharepoint.Client and Sharepoint.Client.Runtime assemblies. and CRM online gave me a hard time referencing third party assemblies (even if I tried to use ILMerge)
Here goes the code for creating folders through Lists.asmx webservice :

For this code to run, you need to add web-reference for lists.asmx.

 

static string FOLDER_EXISTS = “0x8107090d”;
static string SUCCESS = “0x00000000”;
string newFolderName = “test folder name”;
string rootFolderForSharepoint = “”;
string folder1 = <subfolder 1 to be created inside newFoldername;
string folder2 = <subfolder 2 to be created inside newFoldername;
string folder3 = <subfolder 3 to be created inside newFoldername;
string folder4 = <subfolder 4 to be created inside newFoldername;
string rootFolderName = ;
using (Lists listService = new Lists())
{
listService.Url = ;
System.Net.CredentialCache _myCache = new System.Net.CredentialCache();

string xmlNewCommand = “1SubFolder” + newFolderName + ” ” +
“New”;

XmlDocument doc = new XmlDocument();

XmlElement ele = doc.CreateElement(“Batch”);
ele.SetAttribute(“OnError”, “Continue”);
//ele.SetAttribute(“RootFolder”, rootFolderForSharepoint );
ele.InnerXml = xmlNewCommand;
_tracing.Trace(“Before getting error”);
XmlNode node1 = null;
try
{
_tracing.Trace(“Before get”);

XmlNode test = listService.GetListCollection();
_tracing.Trace(test.InnerXml);

node1 = listService.UpdateListItems(rootFolderName , ele);
}
catch (Exception ex)
{

_tracing.Trace(ex.Message.ToString());
}
_tracing.Trace(“Node name” + node1.ToString());

if ((node1 != null) && (node1.FirstChild.FirstChild.InnerText == FOLDER_EXISTS) || (node1.FirstChild.FirstChild.InnerText == SUCCESS))
{
string folder1 = “1. Presentations”, folder2 = “2. Opportunities”, folder4 = “3. Contracts”, folder5 = “4. Projects”; //
// success
string xmlfolder1Command = “1SubFolder” + folder1 + ” ” +
“New”;
string xmlfolder2Command = “1SubFolder” + folder2 + ” ” +
“New”;

string xmlfolder4Command = “1SubFolder” + folder4 + ” ” +
“New”;
string xmlfolder5Command = “1SubFolder” + folder5 + ” ” +
“New”;

XmlDocument docSubFolder = new XmlDocument();

XmlElement eleSubFolder1 = doc.CreateElement(“Batch”);
eleSubFolder1.SetAttribute(“RootFolder”, rootFolderForSharepoint + newFolderName);
eleSubFolder1.InnerXml += xmlfolder1Command;
XmlNode nodeSubFolders1 = listService.UpdateListItems(rootFolderName , eleSubFolder1);

XmlElement eleSubFolder2 = doc.CreateElement(“Batch”);
eleSubFolder2.SetAttribute(“RootFolder”, rootFolderForSharepoint + newFolderName);
eleSubFolder2.InnerXml += xmlfolder2Command;
XmlNode nodeSubFolders2 = listService.UpdateListItems(rootFolderName , eleSubFolder2);
if ((nodeSubFolders2 != null) && (nodeSubFolders2.FirstChild.FirstChild.InnerText == FOLDER_EXISTS) || (nodeSubFolders2.FirstChild.FirstChild.InnerText == SUCCESS))
{

}

XmlElement eleSubFolder4 = doc.CreateElement(“Batch”);
eleSubFolder4.SetAttribute(“RootFolder”, rootFolderForSharepoint + newFolderName);
eleSubFolder4.InnerXml += xmlfolder4Command;
XmlNode nodeSubFolders4 = listService.UpdateListItems(rootFolderName , eleSubFolder4);

XmlElement eleSubFolder5 = doc.CreateElement(“Batch”);
eleSubFolder5.SetAttribute(“RootFolder”, rootFolderForSharepoint + newFolderName);
eleSubFolder5.InnerXml += xmlfolder5Command;
XmlNode nodeSubFolders5 = listService.UpdateListItems(rootFolderName , eleSubFolder5);

}
else
{
//failure
throw new InvalidPluginExecutionException(“Create new folder failed for: ” + newFolderName + “. Error Details: ” + node1.OuterXml);
}
}
Hope this helps someone 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s