【问题标题】:How to write to Sharepoint 2013 list using the Rest API如何使用 Rest API 写入 Sharepoint 2013 列表
【发布时间】:2016-08-16 03:59:31
【问题描述】:

我已经被困了将近一个星期,我想使用他们提供的其余 api 写入 SharePoint 列表。 api 看起来像这样,http://site/_api/lists,从这里我可以根据我附加到我的 url 的内容进行读写,我可以毫无问题地从列表中读取,但是当我必须编写时我会遇到问题。

当我写入列表时,我应该发送 Content-Type、Accept、X-requestDigest 标头和帖子正文。我的代码

NSString *deviceToken = [self getDeviceTokenFromCoreData];
    NSString *postData =  [NSString stringWithFormat:@"{ \"__metadata\": { \"type\": \"SP.Data.TestListItem\" }, \"Title\": \"Test Title\" }"];

    NSData *methodBodyData = [postData dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error;
    NSData *jsonString = [NSJSONSerialization JSONObjectWithData:methodBodyData options:0 error:&error];
    NSString *acceptType = @"application/json;data=verbose";
    NSString *requestDigest = _requestDigest;


    NSURL *subscribeURL = [[NSURL alloc] initWithString:subscribeUrlString];
    NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] initWithURL:subscribeURL];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody:jsonString];
    [theRequest setValue:acceptType forHTTPHeaderField:@"Accept"];
    [theRequest setValue:acceptType forHTTPHeaderField:@"Content-Type"];
    [theRequest setValue:requestDigest forHTTPHeaderField:@"X-RequestDigest"];

这是我为请求构建标头的地方。这就是我处理请求发送的方式

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:theRequest];

    [operation setWillSendRequestForAuthenticationChallengeBlock:^(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge) {
//        if (challenge.previousFailureCount == 0) {

        NSLog(@"%@", challenge.protectionSpace);
            NSURLCredential *creds = [NSURLCredential credentialWithUser:userName
                                                                password:userPass
                                                             persistence:NSURLCredentialPersistenceForSession];

            [[challenge sender] useCredential:creds forAuthenticationChallenge:challenge];
        } else {
            [[challenge sender] cancelAuthenticationChallenge:challenge];
        }
    }];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        //Handle Success

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

       //Handle failure
    }];

    [operation start];
}

这发生在我对 sharepoint 进行身份验证后,我在调试过程中注意到 setWillSendRequestForAuthenticationChallengeBlock 再也不会被调用,看起来我现在需要通过标头发送身份验证信息,这就是我认为请求摘要的用途,但是这无济于事,因为我仍然没有通过。

我从服务器得到的错误信息是

<?xml version="1.0" encoding="utf-8"?>
<m:error 
    xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
    <m:code>-2130575251, Microsoft.SharePoint.SPException</m:code>
    <m:message xml:lang="en-US">The security validation for this page is invalid and might be corrupted. Please use your web browser's Back button to try your operation again.</m:message>
</m:error>

提前致谢:)

【问题讨论】:

  • 请求摘要每 30 分钟左右过期一次。您可以通过从任何 POST 调用的响应标头中获取一个新的。
  • 您能否发布有关如何获取请求摘要的代码?当我尝试调用 site/_api/contextinfo 时,我得到一个 403 禁止的空正文

标签: ios objective-c rest sharepoint-2013


【解决方案1】:

我在互联网上找不到任何答案。我以非推荐的方式解决了。我使用 GET 方法向共享点应用程序中的页面提出申请。在返回中,我有一个带有请求摘要值的标签。 我在我的 POST 申请中以这种方式使用了它:

[headers setValue:@"0x175481C0D6D79A7534A0992E528A5B7D36C80C41C01CBEE55EFB256FA99E1EF551F755BAAE07E692ADE757290F1ACCA11B560F71338DE4AA7781ADC90CDC5249,11 Jun 2015 18:22:18 -0000" forKey:@"X-RequestDigest"];

【讨论】:

  • 但是请求摘要经常变化,你不能硬编码它..@thiago-raiz
【解决方案2】:

我对编码非常陌生,所以很可能有更好的方法,但这就是我解决这个问题的方法:

编写一个从网站检索请求摘要的方法,如下所示:

-(NSDictionary *)digestValue
{
NSURL *url = [NSURL URLWithString:@"https://.../sites/_api/contextinfo"];

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];

NSArray* cookieArray = [NSArray arrayWithObjects: rtFaCookie, fedAuthCookie, nil];
NSDictionary * cookieHeaders = [NSHTTPCookie requestHeaderFieldsWithCookies:cookieArray];
NSMutableDictionary * requestHeaders = [[NSMutableDictionary alloc] initWithDictionary: cookieHeaders];
[requestHeaders setObject: @"application/json;odata=nometadata" forKey: @"Accept"];
[requestHeaders setObject:@"application/json;odata=verbose"    forKey:@"Content-Type"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:@"2" forHTTPHeaderField:@"Content-Length"];
[theRequest setAllHTTPHeaderFields:requestHeaders];

 NSURLResponse *response;
 NSError *error;
 NSData *data = [NSURLConnection sendSynchronousRequest:theRequest  returningResponse:&response error:&error];
 if (data) {
 NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

 return [jsonString JSONValue];
 }

 return nil;

}

然后在您需要有效的请求摘要时调用该方法,如下所示:

  //Method Pulls the most current Digest Value from SharePoint, and pulls out just the Form Digest Value Key.
NSDictionary * taskMetas = [self digestValue];
NSString *formDigestValue = [taskMetas objectForKey:@"FormDigestValue"];

【讨论】:

    猜你喜欢
    • 2015-11-30
    • 2016-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多