Programming against Sharepoint (Online) Client Side Object Model CSOM
26-Oct-1313 Leave a comment
References
Add References to:
- C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll
- C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll
Problem 1
The referenced assembly “Microsoft.SharePoint.Client, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c, processorArchitecture=MSIL” could not be resolved because it has a dependency on “System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a” which is not in the currently targeted framework “.NETFramework,Version=v4.0,Profile=Client”. Please remove references to assemblies not in the targeted framework or consider retargeting your project. SharepointWinFormsCSharp
Solution 1
Ensure that the project targets .Net Framework 4 and not Client Profile
Project > Properties > Application > Target Framework
Problem 2
The type or namespace name ‘SharepointOnlineCredentials’ does not exist in the namespace ‘Microsoft.SharePoint.Client’ (are you missing an assembly reference?)
Solution 2A – use this
First check that the spelling is precise including casing
Second, odd, but I copied the references to the bin folder and re-directed the reference to there and it started working. Odd thing was when I then pointed the reference back it continued to work.
Solution 2B – just creates a new problem
- Select Reference Microsoft.Sharepoint.Client
- Right-Click > Properties
- Embed Interop Types change from False to True
- Build
This returns new problem
- Cannot embed interop types from assembly ‘c:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.Sharepoint.Client.dll” because it is missing either the ‘ImportedFromTypeLibAttribute’ attribute or the ‘PrimaryInteropAssemblyAsstribute’ attribute
- Cannot embed interop types from assembly ‘c:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.Sharepoint.Client.dll” because it is missing the ‘GuidAttribute’ attribute
How to use CSOM
After the above each of the use cases in the following demonstrations then worked.
How to: Complete basic operations using SharePoint 2013 client library code
using (var context = new ClientContext("https://xxx.sharepoint.com")) { var web = context.Web; var login = "xx@xx"; var password = "xxxx"; var securePassword = new System.Security.SecureString(); foreach (char c in password) { securePassword.AppendChar(c); } context.Credentials = new SharePointOnlineCredentials(login, securePassword); context.Load(web); context.ExecuteQuery(); ResusltsListBox.Items.Add(web.Title); }
The end