2
votes

We have a Discussion Board called "Team Discussion" I am trying to create a Discussion Board Item using the SharePoint 2013 Restful API. I can successfully create the Discussion Board Item using following,

Url to which i am posting (url contains the guid for "Team Discussion" Discussion List

http://OurDomain/sites/99812/40242/23653/_api/Web/Lists(guid'c29cef95-6450-4433-8ac8-72d46c9a7fe7')/Items

Headers

accept: application/json;odata=verbose X-RequestDigest: 0x98B316961381BB37DD23B8A1F62EAF853584F74F4307744280198682E0B76AB9446D4480FD01F1E27C0AC9DD412BACA2992F0542D49CD1B009CC211BE94DBDCD,31 Jul 2014 02:13:14 -0000 Content-Type: application/json;odata=verbose

Body

{ '__metadata': { 'type': 'SP.Data.Team_x0020_DiscussionItem' }, 'Title': 'My Team Discussion Item' }

However the programatically created Discussion Board Item doesn't look same as manually created ones. When i go to discussion item, the title is displayed like "Team Discussion : 47_.000"

It seems like i am not setting the correct meta data for SharePoint to create it correctly. I even tried to create a List resource instead of a list item and setting BaseTemplate to 108 and BaseType to value 3. It still creates the Discussion item with the same issue. I also found out that a similar issue exists unless if you use SPUtility.CreateNewDiscussion to create discussion items using Server API.

Any help will be greatly appreciated on how to correct this issue whe using the Resftul API

Regards

Nuwan

1
hey hi..hv you get solution for get All replies of Questions .?? - Vishal Thakkar

1 Answers

0
votes

It occurs since you are creating a discussion item via SP.ListItemCollection resource endpoint. Discussion item in fact is a SP.Folder resource and in that case SP.Folder.Name is assigned to value <Id>.000.

The below example demonstrates how to create a discussion item properly:

var listTitle = "Discussions";
var webUrl = _spPageContextInfo.webAbsoluteUrl;
var payload = {
   '__metadata': { "type": "SP.Data.DiscussionsListItem" },  
   'Title': "Discussion 123",  
   'FileSystemObjectType': 1
};


createNewDiscussion(webUrl,listTitle,payload)
.done(function(item)
{
    console.log('Discussion has been created');
})
.fail(function(error){
    console.log(JSON.stringify(error));
});

where

function executeJson(options) 
{
    var headers = options.headers || {};
    var method = options.method || "GET";
    headers["Accept"] = "application/json;odata=verbose";
    if(options.method == "POST") {
        headers["X-RequestDigest"] = $("#__REQUESTDIGEST").val();
    }   

    var ajaxOptions = 
    {       
       url: options.url,   
       type: method,  
       contentType: "application/json;odata=verbose",
       headers: headers
    };
    if("data" in options) {
      ajaxOptions.data = JSON.stringify(options.data);
    }  

    return $.ajax(ajaxOptions);
}


function createListItem(webUrl,listTitle,payload){
    var url = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/items";
    return executeJson({
        "url" :url,
        "method": 'POST',
        "data": payload
    });
}


function fixDiscussionFolder(webUrl,listTitle,payload){
    var url = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/items(" + payload.Id + ")";
    var payload = {
       '__metadata': payload.__metadata,  
       'FileLeafRef' : payload.Title,
    };
    return executeJson({
        'url' :url,
        'headers': {
            'X-HTTP-Method': 'MERGE',
            'If-Match': '*'
        },
        'method': "POST",
        'data': payload
    });
}