locked
Sharepoint 2013: Create new list item using REST not working consistently RRS feed

  • Question

  • Greetings,
    I have a local intranet hosted SP site and a Microsoft 365 SP site for validation. I've successfully used the REST api on the Microsoft 365-SP hosted site to create a new list item. Using the exact same method, it does not run on the local intranet hosted SP site. I’ve reviewed the ‘Site Settings’ – ‘Site Features’ and they appear correct.
    Any suggestions?

    Note - I'm using SPD. Trying to keep this OOTB.

    Dictionary = request header
    Accept : application/json;odata=verbose
    Content-Type : application/json;odata=verbose
    content-length: 255
    X-RequestDigest: achieved by POST - [Site URL]_api/contextinfo and read d/GetContextWebInformation/FormDigestValue
    Dictionary = _metadata 
    type: SP.Data.[VarListName]ListItem
    Dictionary = request content
    __metadata: _metadata 
    Title: [title variable]
    POST - [site URL]_api/web/lists/getbytitle(‘VarListName’)/items

    Kind regards,




    • Edited by MaxKante Tuesday, November 29, 2016 9:18 PM typo
    Wednesday, November 23, 2016 6:32 PM

Answers

  • Hi Max,

    Any progress now?

    Best regards,

    Patrick


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Marked as answer by MaxKante Wednesday, December 7, 2016 3:42 PM
    Wednesday, December 7, 2016 9:34 AM

All replies

  • Hi

    Can you please check the below

    https://social.msdn.microsoft.com/Forums/windows/en-US/a9b02ada-ef1d-4305-a2ff-09629259dc6f/rest-api-to-create-a-list-item-inside-a-folder-sp-2013?forum=sharepointdevelopment


    Please remember to click Mark as Answer on the answer if it helps you

    Wednesday, November 23, 2016 7:55 PM
  • OK done.

    Note - I'm a web developer and I don't have access to the server side of things.

    From the links, all I can see out of the ordinary is the requirement for = Authorization: "Bearer " + accessToken. I thought this was handled by SPD automatically. If this is not the case, please let me know. Where do i get the access token?

    Anything specific you had wished me to review? The rest API works on the microsoft hosted site, just not the intranet site. Does 365 operate differently?

    Kind regards,

    Wednesday, November 23, 2016 9:03 PM
  • Hi,

    The following code for your reference, please add the code into a content editor web part in a web part page.

    <script src="//code.jquery.com/jquery-3.1.0.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    function AddListItem(){
    
        var listName="VarListName";
        var itemType = GetItemTypeForListName(listName);
        var item = {
            "__metadata": { "type": itemType },
            "Title": "Test"
        };
    	
        $.ajax({
            url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items",
            type: "POST",
            contentType: "application/json;odata=verbose",
            data: JSON.stringify(item),
            headers: {
                "Accept": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val()
            },
            success: function (data) {
                alert('Success');
                window.location.href=window.location.href;
            },
            error: function (data) {
                alert("Error");
            }
        });
    }
    
    
    // Get List Item Type metadata
    function GetItemTypeForListName(name) {
        return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "ListItem";
    }    
    </script>
    
    <input type="button" onclick="AddListItem()" value="Add"/>

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, November 24, 2016 6:58 AM
  • Dennis,

    Ok. It works. I'll investigate further. Thanks.

    Thursday, November 24, 2016 3:03 PM
  • Hi Max,

    If my reply helps you, please mark the reply as answer, it will make others who stuck with the similar issue easier to search for valid solutions in this forum.

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Friday, November 25, 2016 1:26 AM
  • Dennis,

    Thanks, but this did not help overall.

    Its interesting I can perform the same api call using java but not succeed with SPD. I need to get this working with SPD, this needs to work using OOTB. So I'm still stuck as to why one server works and the other one does not. But I'm still investigating.....

    Kind regards,

    Friday, November 25, 2016 5:06 PM
  • Did you try to log the Response variable and response code after the HTTP call step in your SPD workflow? If yes, what is the response? I believe that is the first step for troubleshooting, followed by monitoring the HTTP traffic using fiddler. 

    https://msdn.microsoft.com/en-us/library/office/dn508412.aspx#sectionSection5

    Friday, November 25, 2016 7:12 PM
  • Response Code = BadRequest

    Response Content = This is what I would like to read. In SPD, how can I log the error content? I need to convert the dictionary contents to string prior to logging, but I do not know how. Can you suggest a method?

    I tried this site: Workflow – Troubleshooting REST calls

    Kind regards,

    Friday, November 25, 2016 8:23 PM
  • Hi,

    If you want to add item using SharePoint designer workflow with "Call HTTP service" action, the following article for your reference:

    Using SharePoint REST services from workflow with POST method

    http://mysharepointinsight.blogspot.sg/2013/05/using-sharepoint-rest-services-from.html

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com


    • Edited by Dennis Guo Wednesday, November 30, 2016 8:54 AM
    Tuesday, November 29, 2016 7:13 AM
  • Dennis,

    I am familiar with that site. That sample works on the Microsoft hosted 365 SP site, but not on my intranet hosted site. The response status code = BadRequest. 

    I'd like to troubleshoot this but not sure where to start. I would like to provide the IT group some direction.... if the issue is on their end. 

    Kind regards,

    Tuesday, November 29, 2016 2:39 PM
  • Max,

    I created a workflow in my on-prem SP following the above link and it works. Have you tried to perform a HTTP GET action in your local SP site/lists?

    Also, in dictionary _metadata ,"type" should be all small. "T" is in caps in your first post. 

    Tuesday, November 29, 2016 7:35 PM
  • HTTP Get actions work fine. i can get workflow GUID, list GUIDS, item ID, etc... I can POST to get the X-RequestDigest. I've hard coded everything, no variables. 

    - Thanks

    Note - Confirmed the _metadata ,"type" was lower case. No change.
    Note - 
    Updated initial post to reflect lower case.

    Tuesday, November 29, 2016 9:33 PM
  • Hi,

    Please log the responsecontent to the workflow history list, and see the error messages.

    Or troubleshoot this using Fiddler.

    More on SharePoint 2013 REST API with Fiddler and SPD

    http://www.fabiangwilliams.com/2013/09/03/more-on-sharepoint-2013-rest-api-with-fiddler-and-spd/

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, November 30, 2016 9:01 AM
  • Dennis,

    I want to log the response content but I don't know how (see above). The response content is in dictionary format, and locks up the workflow if I try to log the content dictionary directly. I think I need to choose the dictionary item, but I don't know which one, or the dictionary format to get the item. 

    I'll try and play with Fiddler a bit more. 

    Thanks,

    Wednesday, November 30, 2016 4:29 PM
  • Max,

    What is the authentication type for your on-prem SharePoint? I am thinking about the possible reasons for your issue and one things that pops in mind is the authentication. 

    Also, maybe you have tried it already as trial and error, but try removing X-RequestDigest from your first dictionary object and see if it is effecting the outcome.

    Friday, December 2, 2016 8:25 PM
  • Sampad,

    Authentication type - I've been in formed its a claim based authentication. Let me know if you require something additional.

    Removed X-RequestDigest - no improvement.

    Kind regards,

    Friday, December 2, 2016 9:32 PM
  •   since the blogspot website's contents can be removed at any point of time in future, am posting the contents of the site that Dennis has provided for the benefits of entire community. 

    **Credit goes to Dennis Guo**

    Using SharePoint REST services from workflow with POST method

    SharePoint 2013 has a completely new workflow engine. With that engine we got some new actions and options that help us in creating our bussiness process using workflows. One of them is Call HTTP serviceaction that enables us to leverage an external web service from our workflow.
    On the other hand Sharepoint 2013 comes with a new REST interface that enables us to access and use its resources.When we combine these two new features we get powerful new ways for using our SharePoint workflows.

    In this post I will show you how you can use this new action to POST new data to your SharePoint site.

    I have created a SharePoint list  named WorkflowItems to use for example. We will create items in this list from a workflow using SharePoint REST services. To keep it simple I will leave it with only one column (Title).

    Now we can open our SharePoint Designer and create a new site workflow and call it WorkflowItemWizard.

    In our new workflow first we will create couple of dictionaries. Dictionary is a new variable type in SharePoint workflows that is used for defining collections of key/pair values and is very important for the Call HTTP service action.

    To create a dictionary add Build Dictionary action to you workflow. Using that action specify two new keys Accept and Content-Type and set the values for both of them to application/json; odata=verbose like in the image below

    Set the output to a dictionary with a more meaningful name, call it "header". This dictionary will be used as a header for our request.

    Next create another dictionary that you will call metadata and in it add a key called type with value SP.Data.WorkflowItemsListItem. 

    And finally you will need one more dictionary that will contain your request data. Call it parameters, add key __metadata and set its value to the previously created dictionary metadata

    Add one more key, call it Title and set its value to New item created from workflow.
    Now you are ready to call the web service. Insert action Call HTTP service.
    The URL you will require will look like this:
    https://<site>/<web>/_api/web/lists/getbytitle('WorkflowItems')/items 
    and the HTTP method is POST.

    Set the request to your parameters dictionary. To set the header of your request go to :
    right click on the Call HTTP service -> properties -> RequestHeaders and set it to your header dictionary.

    Now you can deploy your workflow and run it. Once it is over you should have a new item created in your list.
    From this example you can continue building your solutions, you can add more fields, or use other available REST services for updating list items or deleting them. 

    One of the biggest advantages using this method to create or update list items is it is not limited to the site in which you workflow runs. Using REST services you can now access and create items in lists on other sites.

    If you want to know more about SharePoint 2013 REST you should check this out :
    Programming using the SharePoint 2013 REST service

    Also check this excellent post on getting data into your workflow from SharePoint

    https://sergeluca.wordpress.com/2013/04/09/calling-the-sharepoint-2013-rest-api-from-a-sharepoint-designer-workflow/



    Das





    • Edited by DADasRF Sunday, December 4, 2016 10:03 AM
    Sunday, December 4, 2016 1:08 AM
  • Hi Max,

    Any progress now?

    Best regards,

    Patrick


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Marked as answer by MaxKante Wednesday, December 7, 2016 3:42 PM
    Wednesday, December 7, 2016 9:34 AM
  • The anomaly still persists. HTTP Post to create a list item works on the MS 365 SP, but not on the locally hosted SP.

    I guess this is s dead end, and goes into the 'one of those things' pile.

    Wednesday, December 7, 2016 3:41 PM