This is the last of 3 posts describing how to add SharePoint document locations into CRM.
Part 1
Part 2
This code should be added to that in the previous blog post. I had a devil of a job with a 401 error, but this finally cracked it.
Add a web reference to
http://server01/_vti_bin/Lists.asmx and name the reference listservice
http://server01/_vti_bin/Views.asmx and name the reference views
Add a using statement
using system.Xml;
Add this method into the code in previous blog post.
private static void CreateSharePointFolder(string docfolderUrl)
{
if (docfolderUrl == String.Empty || docfolderUrl.IndexOf("/") == -1)
{
return;
}
try
{
// last part is the folder name
string folderName = docfolderUrl.Substring(docfolderUrl.LastIndexOf("/") + 1);
// remove the folder name
docfolderUrl = docfolderUrl.Replace("/" + folderName, "");
// get the document libray name
string docLib = docfolderUrl.Substring(docfolderUrl.LastIndexOf("/") + 1);
// now remove the doc lib to leave the sharepoint site url
string sharePointSiteUrl = docfolderUrl.Replace("/" + docLib, "");
listservice.Lists myLists = new listservice.Lists();
views.Views myViews = new views.Views();
myLists.Url = sharePointSiteUrl + "/_vti_bin/lists.asmx";
myViews.Url = sharePointSiteUrl + "/_vti_bin/views.asmx";
myLists.UseDefaultCredentials = true;
myViews.UseDefaultCredentials = true;
XmlNode viewCol = myViews.GetViewCollection(docLib);
XmlNode viewNode = viewCol.SelectSingleNode("*[@DisplayName='All Documents']");
string viewName = viewNode.Attributes["Name"].Value.ToString();
/*Get Name attribute values (GUIDs) for list and view. */
System.Xml.XmlNode ndListView = myLists.GetListAndView(docLib, viewName);
/*Get Name attribute values (GUIDs) for list and view. */
string strListID = ndListView.ChildNodes[0].Attributes["Name"].Value;
string strViewID = ndListView.ChildNodes[1].Attributes["Name"].Value;
// load the CAML query
XmlDocument doc = new XmlDocument();
string xmlCommand;
xmlCommand = "<Method ID='1' Cmd='New'><Field Name='FSObjType'>1</Field><Field Name='BaseName'>" + folderName + "</Field> <Field Name='ID'>New</Field></Method>";
XmlElement ele = doc.CreateElement("Batch");
ele.SetAttribute("OnError", "Continue");
ele.SetAttribute("ListVersion", "1");
ele.SetAttribute("ViewName", strViewID);
ele.InnerXml = xmlCommand;
XmlNode resultNode = myLists.UpdateListItems(strListID, ele);
// check for errors
NameTable nt = new NameTable();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
nsmgr.AddNamespace("tns", "http://schemas.microsoft.com/sharepoint/soap/");
if (resultNode != null)
{ // look for error text in case of duplicate folder or invalid folder name
XmlNode errNode = resultNode.SelectSingleNode("tns:Result/tns:ErrorText", nsmgr);
if (errNode != null)
{
// Write error to log;
}
}
}
catch (Exception ex)
{
throw ex ;
}
}
6 comments:
Thank you, just passed a link to your post along to a developer who needs to create Sharepoint folders using Web Service calls. I think he'll find the information helpful. Have a good day!
Hello,
I am receiving this error when i try to debug the code "System.Web.Services.Protocols.SoapException: Exception of type Microsoft.SharePoint.SoapServer.SoapServerException was thrown." can you help me on this?
I am trying to run your code. I am having an issue with the web service references.
listservice.Lists myLists = new listservice.Lists();
views.Views myViews = new views.Views();
My listservice doesn't have a Lists class. My views doesn't have a Views class.
I am using the following URL for views.
http://apr-tst-crm2/sites/CRM//_vti_bin/Views.asmx
When I look at that code, I see ViewSoap, ViewSoapChannel, ViewSoapClient.
Am I missing something?
I tried
http://apr-tst-crm2/_vti_bin/Views.asmx
as well, but still now Views class.
Nevermind. I figured it out. I was adding a Service Reference instead of a Web Reference. All fixed now.
Post a Comment