Question connect to remote Sharepoint (office 365) and add documents programmatically. Issues with DLLS

gsaunders

Member
Joined
Mar 30, 2021
Messages
16
Programming Experience
5-10
Hello,

Using Visual Studio 2019.
I need to connect to one of our Office 365 Sharepoint Sites and have the ability to simply add files via code.

I am ONLY connect to the remote sharepoint site and do not have sharepoint server running on my Windows 10 OS.

I get an error on the very first step which is:
Connect to Sharepoint SIte:
        private void button1_Click(object sender, EventArgs e)
        {
            //Retrive sitecollection
            // Get Errors when this runs
            using (SPSite site = new SPSite("https://xxxxx.sharepoint.com/sites/sitename"))
            {
                //Open Web
                using (SPWeb web = site.OpenWeb())
                {

                    MessageBox.Show(web.Description, "Title");

                    //get library named "Documents"
                    SPList DocLib = web.Lists["DVBCI"];
                }

            }
        }

From what I have seen I needed to have the Microsoft.Sharepoint dll so I used the Manage Nuget Packages and installed:
  • Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c
I build, run the application and then click my button and get the following error:

System.IO.FileNotFoundException
HResult=0x80070002
Message=Could not load file or assembly 'Microsoft.SharePoint.Library, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' or one of its dependencies. The system cannot find the file specified.
Source=Microsoft.SharePoint
StackTrace:
at Microsoft.SharePoint.CoreResource.GetString(ResourceGroup rg, String name, Object[] values)
at Microsoft.SharePoint.SPSite.LookupSiteInfo(SPFarm farm, Boolean contextSite, Boolean swapSchemeForPathBasedSites, Uri& requestUri, Boolean& lookupRequiredContext, Guid& applicationId, Guid& contentDatabaseId, Guid& siteId, Guid& siteSubscriptionId, SPUrlZone& zone, String& serverRelativeUrl, Boolean& hostHeaderIsSiteName, Boolean& appWebRequest, String& appHostHeaderRedirectDomain, String& appSiteDomainPrefix, String& subscriptionName, String& appSiteDomainId, Uri& primaryUri)
at Microsoft.SharePoint.SPSite..ctor(SPFarm farm, Uri requestUri, Boolean contextSite, Boolean swapSchemeForPathBasedSites, SPUserToken userToken)
at Microsoft.SharePoint.SPSite..ctor(String requestUrl)
at DocVaultTransfer.Form1.button1_Click(Object sender, EventArgs e) in C:\VS2019\Repo\DocVaultTransfer\Form1.cs:line 27
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at DocVaultTransfer.Program.Main() in C:\VS2019\Repo\DocVaultTransfer\Program.cs:line 19

I have verified the file, the version and everything exists.

So I then used the Fuslogvw.exe tool to try and find what assemblies are not loading.

So found an entry saying it could not find Microsoft.SharePoint.Security.DLL.

I then used Nuget to download that to the solution and built/ran again.

This time fails again, but now says it could not find Microsoft.SharePoint.intl.

I am searching in Nuget again and not finding this.

So at this point I am at a loss as to what to do.

Do I have to have sharepoint itself installed on the local dev unit???

One... am I even using the correct commands to connect to the site and work with the library and so forth.

There are a lot of other nuget sharepoint type of packages, but now I am just guessing at what may be needed.

Thanks in advance for your help!
 
Solution
There's one primary issue. The code you are using there is for on-premise SharePoint, and is expected to be run on the SharePoint server itself. You can't do that with SharePoint Online, because for obvious reasons, Microsoft doesn't random Joe Schmoe uploading code directly unto their SharePoint servers and executing code.

You need to use the SharePoint CSOM library. CSOM standards for Client Side Object Model. Or you can choose to use the Patterns & Practices SharePoint library. Or if you need to do is an upload, you may be able to get by using the REST APIs, or the Graph API.

...
I starting to think the site that pointed me to doing this sent me down the wrong path.

Perhaps I should be using the Sharepoint Client Object Model (SCOM).

Doing some research here as it is possible the above methodology was for server side work and not remote as a client.
 
There's one primary issue. The code you are using there is for on-premise SharePoint, and is expected to be run on the SharePoint server itself. You can't do that with SharePoint Online, because for obvious reasons, Microsoft doesn't random Joe Schmoe uploading code directly unto their SharePoint servers and executing code.

You need to use the SharePoint CSOM library. CSOM standards for Client Side Object Model. Or you can choose to use the Patterns & Practices SharePoint library. Or if you need to do is an upload, you may be able to get by using the REST APIs, or the Graph API.

 
Solution
There's one primary issue. The code you are using there is for on-premise SharePoint, and is expected to be run on the SharePoint server itself. You can't do that with SharePoint Online, because for obvious reasons, Microsoft doesn't random Joe Schmoe uploading code directly unto their SharePoint servers and executing code.

You need to use the SharePoint CSOM library. CSOM standards for Client Side Object Model. Or you can choose to use the Patterns & Practices SharePoint library. Or if you need to do is an upload, you may be able to get by using the REST APIs, or the Graph API.

Thanks for your quick feedback.

I had hit several sites directing me the way I went, but it did start to seem geared to server side.

I think this gets me in the correct direction.

Thanks!
 

Latest posts

Back
Top Bottom