对于在 SharePoint 2013 和 Online 中开发 Web 应用程序,您有 2 个主要选项可用于从列表、库或用户详细信息中查询数据,即客户端对象模型和 SharePoint REST API。
这是一个使用Client object model更新列表数据的示例
ClientContext context = new ClientContext("http://SiteUrl");
List announcementsList = context.Web.Lists.GetByTitle("Announcements");
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem newItem = announcementsList.AddItem(itemCreateInfo);
newItem["Title"] = "My New Item!";
newItem["Body"] = "Hello World!";
newItem.Update();
context.ExecuteQuery();
另一个首选选项是使用 REST API 来查询端点。您可以在 SharePoint 中查询许多 API,最有用的是 Search API 或社交 API、用户配置文件 API 等...
这是一个示例端点,您可以查询以检索 JSON 数据,您可以将其放在浏览器中或发布到 url 以查看返回的内容。
http://<siteCollection>/<site>/_api/social.feed/my/feed/post
这是在 SharePoint 中获取当前用户的用户配置文件数据的示例
$(document).ready(function(){
// Ensure the SP.UserProfiles.js file is loaded
SP.SOD.executeOrDelayUntilScriptLoaded(loadUserData, 'SP.UserProfiles.js');
});
var userProfileProperties;
function loadUserData(){
var clientContext = new SP.ClientContext.get_current();
var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
//Get properties of the current user
userProfileProperties = peopleManager.getMyProperties()
clientContext.load(userProfileProperties);
clientContext.executeQueryAsync(onSuccess, onFail);
}
function onSuccess() {
console.log(userProfileProperties.get_displayName());
}
function onFail(sender, args) {
console.log("Error: " + args.get_message());
}