-1

I've searched a few of the forums on here but to no avail. I have a VBA Project on Microsoft Outlook 2016, and I need to add email information to a SharePoint List. The most straight-forward solution seems to be using Visual C# to connect directly. I have downloaded the Microsoft SharePoint ISAPI web server extensions. My compiler (csc) is version 4.7.2046.0 running C# 5 (I know it's not the latest version).

using System;
using Microsoft.SharePoint.Client;

/* Run with:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe /reference:"C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll" /reference:"C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" add.cs
*/

namespace ConsoleApplication
{
class AddToList
{
    public static void InsertEnquiryToSharepoint(string rTest, string rSubject, string rUser, DateTime rDate, string rEmail, string rPhone)
    {

        string siteUrl = "https://myteamsite.com/teams/teamname/";

        ClientContext clientContext = new ClientContext(siteUrl);
        Microsoft.SharePoint.Client.List oList = clientContext.Web.Lists.GetByTitle("ListName");

        ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
        ListItem oListItem = oList.AddItem(itemCreateInfo);

        oListItem["Test"] = rTest;
        oListItem["Subject"] = rSubject;
        oListItem["User"] = rUser;
        oListItem["DateTime"] = rDate;
        oListItem["Email"] = rEmail;
        oListItem["Phone"] = rPhone;

        oListItem.Update();
        clientContext.ExecuteQuery(); 
    }

    static void Main(string[] args)
    {
        InsertEnquiryToSharepoint("Yes", "A test subject", "Kevin Shelley", System.DateTime.Now, "No", "No");
    }
}

}

I am running the command as a test in my command line, and since it is not coming back with any error messages, I can't debug it past execution. Here is the CL command:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe /reference:"C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll" /reference:"C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" add.cs

Since I am writing the initial project in VBA and referencing this, I am willing to utilize a VBA solution that can work inside of Microsoft Outlook 2016. Thanks.

EDIT: I failed and didn't run the executable this created; However, once I did I got the following error and stack trace:

Unhandled Exception: System.Net.WebException: The remote server returned an error: (403) Forbidden.
   at System.Net.HttpWebRequest.GetResponse()
   at Microsoft.SharePoint.Client.SPWebRequestExecutor.Execute()
   at Microsoft.SharePoint.Client.ClientContext.GetFormDigestInfoPrivate()
   at Microsoft.SharePoint.Client.ClientContext.EnsureFormDigest()
   at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()
   at ConsoleApplication.AddToList.InsertEnquiryToSharepoint(String rTest, 
String rSubject, String rUser, DateTime rDate, String rEmail, String rPhone)
   at ConsoleApplication.AddToList.Main(String[] args)

My assumption would be that I required permissions for the site. If so, how do I go about passing credentials safely?

  • That `csc.exe` command is building your app, just like Ctrl+Shift+B (Build Project) does in Visual Studio, ..it doesn't run it. Assuming you're in Visual Studio, have you tried F5-Debug? If you're not in VS, then after running the `csc.exe` command, you should have a `ConsoleApplication.exe` executable ready to run - run it. I suspect you might need to wrap everything in `InsertEnquiryToSharepoint` with a `try...catch` block, and output the exception to the console's error output to see what's going on if you don't have VS to debug. – Mathieu Guindon Mar 29 '18 at 15:35
  • So... are you using Visual Studio or not? – Mathieu Guindon Mar 29 '18 at 15:51
  • Yeah, I'm an idiot for sure on the executable. I found it, and it was compiling correctly. I ran the executable and it came back with an error saying "(403) Forbidden". Is there something I have to add to validate credentials? I'll edit my answer to reflect the stack trace – KevinThePepper23 Mar 29 '18 at 16:14
  • See if the `ClientContext` wouldn't happen to have a way to provide credentials – Mathieu Guindon Mar 29 '18 at 16:16
  • Duplicate of https://stackoverflow.com/q/6382583/1188513 – Mathieu Guindon Mar 29 '18 at 16:53

2 Answers2

0

Assuming the site is inside your network and you have credential integration setup properly, then its just a single command you are missing after you instantiate the object:

ClientContext clientContext = new ClientContext(siteUrl);
clientContext.Credentials = CredentialCache.DefaultNetworkCredentials; // NEW 

This gets the users credentials, meaning the executing user needs to have access (rather than a hard-coded username/password as mentioned in the recommended link above)

Ctznkane525
  • 7,297
  • 3
  • 16
  • 40
0

You need to do authentication of SharePoint.

Here is a demo for your reference:

    /// <summary>
    /// set authentication of SharePoint online
    /// </summary>
    /// <param name="clientContext"></param>
    /// <param name="userName"></param>
    /// <param name="password"></param>
    public static void setOnlineCredential(ClientContext clientContext,string userName,string password)
    {
        //set the user name and password
        SecureString secureString = new SecureString();
        foreach (char c in password.ToCharArray())
        {
            secureString.AppendChar(c);
        }
        clientContext.Credentials = new SharePointOnlineCredentials(userName, secureString);           
    }

    /// <summary>
    /// set authentication of SharePoint on-premise(SharePoint 2010/2013/2016)
    /// </summary>
    /// <param name="clientContext"></param>
    /// <param name="userName"></param>
    /// <param name="password"></param>
    /// <param name="domain"></param>
    public static void setClientCredential(ClientContext clientContext, string userName, string password, string domain)
    {
        clientContext.Credentials = new NetworkCredential(userName, password, domain);
    }
Lee Liu
  • 1,981
  • 1
  • 12
  • 13