مقدمه
به سری REST SharePoint 2013 خوش آمدید. در مقاله قبلی من، نحوه حذف یک پوشه از کتابخانه اسناد در شیرپوینت 2013 با استفاده از REST API را دیدیم.
در این مقاله، نحوه دریافت جزئیات گروه سایت در فهرست شیرپوینت با استفاده از REST API را مورد بحث قرار خواهیم داد.
محیط شیرپوینت 2013 این امکان را به شما می دهد تا با استفاده از REST با سایت های شیرپوینت از راه دور تعامل داشته باشید. بنابراین می توانید با استفاده از هر فناوری که از قابلیت های استاندارد REST پشتیبانی می کند، با اشیاء شیرپوینت صحبت کنید. به این ترتیب داده های شیرپوینت در هر کجا و همه جا قابل دسترسی است.
لیست نقاط دسترسی REST
فهرستی از نقاط دسترسی است که به شما امکان ورود به نقاط دسترسی گرانول را می دهد.
- سایت
http://server/site/_api/site
- وب
http://server/site/_api/web
- نمایه کاربر
http:// server/site/_api/SP.UserProfiles.PeopleManager
http:// server/site/_api/search را جستجو کنید
- انتشار
http:// server/site/_api/publishing
فهرست نقاط پایانی REST
فهرستی از نقاط پایانی که بیشترین استفاده را در لیست شیرپوینت دارند در زیر آمده است.
- http://server/site/_api/web/lists
- http://server/site/_api/lists/getbytitle('listname')
- http://server/site/_api/web/lists('guid')
- http://server/site/_api/web/lists/getbytitle('Title')
توجه: کد زیر در محیط آنلاین SP 2013 من تست شده است.
مرحله 1: قبل از نوشتن کد خود، لطفاً مطمئن شوید که مجوز کافی برای دسترسی به درخواستهای بین دامنهای را دارید. بنابراین من به تمام مطالب فهرست شده در زیر اجازه کامل داده ام.
مستاجر |
مجوز کامل |
مجموعه سایت |
مجوز کامل |
وب |
مجوز کامل |
فهرست کنید |
مجوز کامل |
کد قرار دهید
- 'use strict';
- var hostweburl;
- var appweburl;
- // This code runs when the DOM is ready and creates a context object which is
- // needed to use the SharePoint object model
- $(document).ready(function () {
- //Get the URI decoded URLs.
- hostweburl =
- decodeURIComponent(
- getQueryStringParameter("SPHostUrl"));
- appweburl =
- decodeURIComponent(
- getQueryStringParameter("SPAppWebUrl"));
- // Resources are in URLs in the form:
- // web_url/_layouts/15/resource
- var scriptbase = hostweburl + "/_layouts/15/";
- // Load the js file and continue to load the page with information about the list top level folders.
- // SP.RequestExecutor.js to make cross-domain requests
- // Load the js files and continue to the successHandler
- $.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest);
- });
- // Function to prepare and issue the request to get
- // SharePoint data
- function execCrossDomainRequest() {
- // executor: The RequestExecutor object
- // Initialize the RequestExecutor with the app web URL.
- var executor = new SP.RequestExecutor(appweburl);
- executor.executeAsync(
- {
- url: appweburl + "/_api/SP.AppContextSite(@target)/web/ /sitegroups(1)?@target='" + hostweburl + "'",
- method: "GET",
- headers: { "Accept": "application/json; odata=verbose" },
- success: function (data) {
- alert("success: " + JSON.stringify(data));
- },
- error: function (err) {
- alert("error: " + JSON.stringify(err));
- }
- }
- );
- }
- }
- // This function prepares, loads, and then executes a SharePoint query to get
- // the current users information
- //Utilities
- // Retrieve a query string value.
- // For production purposes you may want to use
- // a library to handle the query string.
- function getQueryStringParameter(paramToRetrieve) {
- var params =
- document.URL.split("?")[1].split("&");
- for (var i = 0; i < params.length; i = i + 1) {
- var singleParam = params[i].split("=");
- if (singleParam[0] == paramToRetrieve)
- return singleParam[1];
- }
- }
- //Retrieve the form digest value.
- //Store the value of the form digest.
- function contextSuccessHandler(data) {
- alert("List Created Successfully");
- }
- function contextErrorHandler(data, errorCode, errorMessage) {
- alert(data);
- alert(errorCode);
- alert("Could not get context info: " + errorMessage);
- }
اسکرین شات
مرحله 3: هنگام استقرار، صفحه زیر از شما خواسته می شود. Trust it را فشار دهید و به استقرار ادامه دهید.
Code Walkthrough
A. روش ارسال در REST API
سرویس SharePoint 2013 REST از ارسال دستورات POST که شامل تعاریف شیء به نقاط پایانی هستند که مجموعه ها را نشان می دهند، پشتیبانی می کند. در این مثال، Test List یک لیست شیرپوینت سفارشی است که در آن موارد لیست به روز می شوند.
ب. درخواست Executor.JS