locked
Iterating over SharePoint folders from CRM Plugin causes strange error. RRS feed

  • Question

  • Here's the picture; In a plugin, I have the URL of a SharePoint folder retrieved from a CRM 2011 Document Location using the RetrieveAbsoluteAndSiteCollectionUrlRequest. It will return something like "https://myserver.mycompany.com/Projects".

    What I want to do is create a subFolder at this location. I have a method that will check the existing subFolders of this location, and, if the name I want to use doesn't exist, will create the subFolder using that name, otherwise it will use a name guaranteed to be unique.

    I've tested the method using a console app connecting to the same SharePoint environment, and it works great. However, when I use the same method in my Plugin, it thorws an error while iterating over the subFolders.

    Here's the code:

    using Microsoft.SharePoint.Client;
    public string CreateFolder(string convenientName, string uniqueGuaranteedName, string parentLocationUrl)
    {
    	Uri parentLocationUri = new Uri(parentLocationUrl);
    	// _clientContext is an existing Microsoft.SharePoint.Client.ClientContext pointing to the SharePoint environment.
    	Folder folder = _clientContext.Web.GetFolderByServerRelativeUrl(parentLocationUri.AbsolutePath);
    	
    	_clientContext.Load(folder.Folders);
    	_clientContext.ExecuteQuery();
    	bool any = false;
    	// folder.Folders is a Microsoft.SharePoint.Client.FolderCollection,
    	// which extends Microsoft.SharePoint.Client.ClientObjectCollection<Folder>
    	// so it should only ever contain Folder objects.
    	foreach (Folder subFolder in folder.Folders)  // <--- It fails here.
    	{
    		if (subFolder.Name == convenientName)
    		{
    			any = true;
    			break;
    		}
    	}
    	string nameUsed = any ? uniqueGuaranteedName : convenientName;
    	folder.Folders.Add(string.Format("{0}/{1}", parentLocationUri.AbsolutePath, nameUsed));
    	folder.Update();
    	_clientContext.ExecuteQuery();
    	return nameUsed;
    }

    When this code runs in a plugin, the Foreach line produces the following error:

    Unable to cast object of type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]' to type 'Microsoft.SharePoint.Client.Folder'.

    I have no idea how this is even possible. I'm at a loss here, and any help would be greatly appreciated.

    Some more information to help figure this out:

    CRM 2011 (5.0.9690.2243)

    .NET 4.0

    I've used the ILMerge tool (http://www.microsoft.com/en-us/download/details.aspx?id=17630) to merge the Microsoft.SharePoint.Client.dll, and Microsoft.SharePoint.Client.Runtime.dll into the Plugin assembly, so there shouldn't be an issue with the environment not having the proper dlls.

    Assembly is NOT registered in the Sandbox.

    Plugin is Registered Post-Operation, Syncronous, Server only.



    • Edited by Nitron_F117 Wednesday, May 16, 2012 5:55 PM
    Wednesday, May 16, 2012 5:51 PM

All replies

  • Are the SharePoint assemblies .Net 4.0 ? I thought the SharePoint assemblies were .Net 3.5, which I'd expect would cause problems (though I wouldn't necessarily expect the particular error that you're getting). To avoid any .Net version issues, I'd be tempted to use the SharePoint web services to commmunication with SharePoint

    Does the console app also use the ILMerged assembly, and .Net 4.0 ? If it has the same problem with the ILMerged assembly, then I think you can avoid ILMerge by putting all assemblies in the server\bin\assembly directory, and registering the plugin on Disk


    Microsoft CRM MVP - http://mscrmuk.blogspot.com  http://www.excitation.co.uk


    Thursday, May 17, 2012 7:59 AM
    Moderator
  • Hi! For those, who are looking for the answer, to resolve this issue, you should use /copyattrs parameter with ILMerge.
    • Proposed as answer by ipriha Wednesday, November 19, 2014 10:07 AM
    Monday, January 28, 2013 3:22 PM