如何自动化SharePoint记录更新

时间:2018-04-02 13:55:15

标签: windows powershell sharepoint

因此,我们有一个库存清单,其中包含每个项目的详细信息,主要是下拉菜单和复选框,一些注释和说明。这些记录在SharePoint中。除了大量其他步骤之外,我们有时需要更新其中的多个项目,并且我尝试自动执行大多数这些步骤,包括更新其SharePoint记录。从任何远程计算机在PowerShell中进行此操作的最佳方法是什么?

我会连接到数据库,找到记录并在那里更新记录吗?有没有更简单的方法?我尝试为SharePoint查找PowerShell CLI工具,但我没有在任何地方看到它们。

例如,我可能想在此处更新此字段:

enter image description here

1 个答案:

答案 0 :(得分:1)

我认为SharePoint中最好的Automate更新列表项是使用CSOM(C#代码)API。

以下是有关更新列表项的演示,供您参考:

// Starting with ClientContext, the constructor requires a URL to the 
// server running SharePoint. 
ClientContext context = new ClientContext("http://SiteUrl"); 

// Assume that the web has a list named "Announcements". 
List announcementsList = context.Web.Lists.GetByTitle("Announcements"); 

// Assume there is a list item with ID=1. 
ListItem listItem = announcementsList.GetItemById(1); 

// Write a new value to the Body field of the Announcement item.
listItem["Body"] = "This is my new value!!"; 
listItem.Update(); 

context.ExecuteQuery();  

要进行身份验证以访问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
    /// </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);
    }

如果您想使用PowerShell,我们可以使用PowerShell来使用CSOM,如本文所述:CSOM SharePoint PowerShell Reference and Example Codes我们只需将上面的代码修改为PowerShell代码