我在这里搜索了一些论坛,但无济于事。我在Microsoft Outlook 2016上有一个VBA项目,我需要将电子邮件信息添加到SharePoint列表中。最直接的解决方案似乎是使用Visual C#直接连接。我已下载Microsoft SharePoint ISAPI Web服务器扩展。我的编译器(csc)是运行C#5的版本4.7.2046.0(我知道它不是最新版本)。
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");
}
}
}
我在命令行中运行该命令作为测试,并且因为它没有返回任何错误消息,所以我无法在执行后调试它。这是CL命令:
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
由于我在VBA中编写初始项目并引用它,我愿意使用可以在Microsoft Outlook 2016中运行的VBA解决方案。谢谢。
编辑:我失败了,并没有运行这个创建的可执行文件;但是,一旦我这样做,我得到以下错误和堆栈跟踪:
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)
我的假设是我需要网站的权限。如果是这样,我如何安全地传递凭证?
答案 0 :(得分:0)
假设该站点位于您的网络中并且您正确设置了凭据集成,那么在您实例化该对象后,它只是您丢失的一个命令:
ClientContext clientContext = new ClientContext(siteUrl);
clientContext.Credentials = CredentialCache.DefaultNetworkCredentials; // NEW
这会获取用户凭据,这意味着正在执行的用户需要具有访问权限(而不是上面推荐链接中提到的硬编码用户名/密码)
答案 1 :(得分:0)
您需要对SharePoint进行身份验证。
这是一个供您参考的演示:
/// <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);
}