Answered

Call WebAPI in Shared Content List when you create a new item

This Post was created on behalf of: Divyata J

------------------------------------------------------------------------------------------------------------

Hi,

I have specific requirement on a shared content list whenever I create a new list item it should call a web api which will give me some number I want to use that number member value of list item(like list item Id ).

Can you tell me if this is possible ? or is there any documentation on it please drive me to right direction.

 

Thanks

0

Comments

2 comments
  • Official comment

    This is a great opportunity to use Custom Fields.

    This means you can create a re-usable field that you can add to ANY content/module definition. So you can code this once and re-use it across the instance.

    There's some examples of custom fields already on Github that you can take a look at to get an idea of how they work.

    Generally, this is how I'd recommend you get started:

    1. Check out the Custom Fields repo/readme on GitHub 

    2. Follow the steps in 'Setup' such as creating/registering your input form customization file

    3. Start with the boilerplate CustomFieldFunctionDeclaration in the readme file

    4. Modify the JS code to look something like this:
      ...
      this
      .ViewModel = function (options) { /// <summary>[Optional] The KO ViewModel that will be binded to your HTML template.</summary> /// <param name="options" type="Object"> /// <field name="$elem" type="jQueryElem">The .field-row jQuery Dom Element.</field> /// <field name="contentItem" type="ContentItem Object">The entire Content Item object including Values and their KO Observable properties of all other fields on the form.</field> /// <field name="fieldBinding" type="KO Observable">The value binding of thie Custom Field Type. Get and set this field's value by using this property.</field> /// <field name="fieldSetting" type="Object">Object representing the field's settings such as 'Hidden', 'Label', and 'Description'</field> /// <field name="readonly" type="boolean">Represents if this field should be readonly or not.</field> /// </param> this.value = options.fieldBinding; //reference the field KO observable value this.contentID = options.contentItem.ContentID; //set the contentID of the current loaded item (NewItem = -1) this.attrBinding = {}; //pass any custom attributes to the input field if (options.fieldSetting.Settings.Required === "True") { //if this field is marked as required, add a required parsley attribute this.attrBinding['data-parsley-required'] = true; }

      //some pseudo code here...
      var self = this;
      //on load, make an ajax request to your external service
      $.get('https://someurl.com', function(data) {
      var id = data.id;
      self.value(id);
      });
      //on error, you'll probably want to do something to capture that... }
      ...

       

    5. For your actual display of the field, if you want this to happen in the background and don't want any actual output, then your .html file for the 'Template' field could just be empty, or you could set it to a text field. Like this:
      <div class="input-group">
      <input type="text" class="form-control" data-bind="value: value, attr: attrBinding" />
      </div>
    6. I might recommend outputting a text input field so that the client validation doesn't allow it to save unless it has a value, then you can set the text input to be disabled so a user can't manually enter a value.

    Let me know if this works for you!

    Comment actions Permalink
  • Hi Divyata,

    I think I understand what you mean, but just want to confirm. Would this be the flow you are looking to accomplish?

    1. User goes to a Shared Content List in Agility
    2. User clicks 'New' to create a new item and the form is displayed
    3. A call to a Web API happens automatically to get a value for something and use that as a form field in the content item
    4. User saves and closes the item.

    I just need a little clarification on exactly what you are trying to do with the web api as there serveral different things you can do for example:
    - Call the WebAPI using Javascript by writing some custom script and saving a value to a content item property
    - You can use a 'webhook' so that AFTER the content item is published, you can syndicate it's content to another service

    Let me know what you are looking to do and I can help guide you through this.

    Regards,
    James

    0
    Comment actions Permalink

Please sign in to leave a comment.

Didn't find what you were looking for?

New post