SP2013: Updating a List using REST in OData format from JavaScript

After wrestling with this tonight for sometime I’ve finally cracked it. SP2013 RTMed
and alot of the sample code fails due to the fact that you need to now add ’..;odata=verbose’
onto pretty much every call to SharePoint.

Basically you get a series of errors such as:

MIME type could not be
found
that matches the content type of
the response
. None of the supported type(s)
application/atom+xml;type=entry, application/atom+xml, application/json;odata=verbose

 

Previously alot of the sample code has

$.getJSON(.) as part of the call to the server – as mentioned we now need to add
some custom header values of ’odata=verbose’, so to save you hours
of slogging on this, the getJSON call doesn’t allow custom header values. You need
to use the $.ajax() for these calls.

READING FROM A LIST

function getCustomers() {
 

  // begin work to call across network
  var requestUri = _spPageContextInfo.webAbsoluteUrl +
               
“/_api/Web/Lists/getByTitle(‘CustomersREST’)/items/” +
               
“?$select=Id,FirstName,Title,WorkPhone” +
               
“&$orderby=Title,FirstName”;
   

  var requestHeaders = {
      “accept”: “application/json;odata=verbose”
  }
    // execute AJAX request

  $.ajax({
      url: requestUri,
      type: ‘GET’,
      dataType: ‘json’,
      headers: requestHeaders,
      success: onDataReturned,
      error: onError
  });
}

 

UPDATING A LIST ITEM

//Sample code to update a Customer List Item in a Customer List called ’CustomersREST’

function updateCustomer(dialogResult, returnValue) {

  if (dialogResult == SP.UI.DialogResult.OK) {
    var Id = returnValue.Id;
    var FirstName = returnValue.FirstName;
    var LastName = returnValue.LastName;
    var WorkPhone = returnValue.WorkPhone;
    var etag = returnValue.etag;

    var requestUri = _spPageContextInfo.webAbsoluteUrl +
              “/_api/Web/Lists/getByTitle(‘CustomersREST’)/items(”
+ Id + “)”;

    var customerData = {
      __metadata: { “type”: “SP.Data.CustomersRESTListItem”
},
      Title: LastName,
      FirstName: FirstName,
      WorkPhone: WorkPhone
    };

    requestBody = JSON.stringify(customerData);

    var requestHeaders = {
        “accept”: “application/json;odata=verbose”,
        “X-RequestDigest”: $(“#__REQUESTDIGEST”).val(),
        “X-HTTP-Method”: “MERGE”,
        “content-length”: requestBody.length,
        “content-type” : “application/json;odata=verbose”,
        “If-Match”: etag
    }

    $.ajax({
      url: requestUri,
      type: “POST”,
      contentType: “application/json;odata=verbose”,
      headers: requestHeaders,
      data: requestBody,
      success: onSuccess,
      error: onError
    });

  }

}

Blog Post by: Mick Badran

Using EDI Bridge to exchange B2B messages in Windows Azure Service Bus

Using EDI Bridge to exchange B2B messages in Windows Azure Service Bus

My last demo will be an Electronic Data Interchange (EDI) Demo: This demo intends to show how to configure trading partners, agreements and the creation of EDI Bridges to process and exchange EDI messages to your partners. We will sends a Sales Order message in an X12 Electronic Data Interchange (EDI) format using the X12 […]
Blog Post by: Sandro Pereira

Creating a MessageBox On-Ramp for the ESB Toolkit in BizTalk 2013

In an environment where Microsoft BizTalk ESB Toolkit is deployed, a BizTalk receive location responsible for receiving ESB-destined messages is referred to as an "on-ramp." In the receive location you have to use one of the pipelines provided as part of the toolkit, and then correctly configure the components of that pipeline to determine the itinerary and link it to the message. So you can use almost any location outside BizTalk as an on-ramp but what if you want to pick up a message from the MessageBox database and use an itinerary to process that message? You can’t use pipeline components once a message is picked up by BizTalk and is already in the MessageBox. Therefore you have to create custom code that must be invoked in an orchestration to perform the steps that are normally made inside the pipeline. The objects that are used inside the ESB Toolkit are not described on MSDN so Reflector is you best friend to figure out which objects have to be used and how to invoke them.

Steps

In the following example I’m going to process a sales order message in BizTalk with an orchestration. In the orchestration is an event message created that is sent to the MessageBox and picked up by an itinerary. (The same itinerary can be used to process multiple event types.)

The following steps are necessary to make it work:

  • Create a custom component in .NET to set the context properties that are needed for the ESB Toolkit
    • In the custom component:
      • Resolve the itinerary from the Itinerary Store database
      • Determine the first Itinerary Service in the itinerary
      • Write the properties of the Itinerary Service as context properties on the message
      • Attach the itinerary to the message
  • Create a map to transform the SalesOrder to an OrderEvent message
  • Create an Orchestration to process SalesOrder messages
    • In the Orchestration:
      • Receive the SalesOrder message
      • Use the map to transform the SalesOrder to an OrderEvent message
      • Set the ESB properties with the custom componen
      • Promote the Create a Correclation Se
      • Send the OrderEvent message to the MessageBox database with a Direct Port
  • Create an itinerary to process Event messages

 

Create a custom component in .NET to set the context properties that are needed for the
ESB Toolkit
Create a class in .NET with a method that receives a XLANGMessage and the name of the itinerary. 
In the method perform the following activities:
– Resolve the itinerary from the Itinerary Store database with the ResolverMgr class.
– Serialize it into an Itinerary object and get the first Itinerary Service from the itinerary.
– Set the necessary properties of the itinerary like beginTime and the interchangeId of the message.
– Write the ServiceName, ServiceType and ServiceState properties of the first Itinerary Service as
   promoted properties on the XLANGMessage
– Write the created Itinerary object as a promoted propery on the XLANGMessage
 
 
Create a map to transform the SalesOrder to an OrderEvent message
 
 
Create an Orchestration to process SalesOrder messages
Create an Orchestration that receives the SalesOrder message, use the map to transform it to an OrderEvent message, set the properties for the ESB Toolkit and send the OrderEvent message to
the MessageBox with a Direct Port
 
Set the ESB properties on the message with the custom .NET component
 
Create a Correlation Set so that context properties on the message are also promoted.
 
Use the Correlation Set in the Send Shape when sending the OrderEvent message to the MessageBox
 
 
Create an itinerary to process Event messages
The first Itinerary Service in the Itinerary must be an orchestration and can’t be a Messaging Service because in fact that are pipeline components. My itinerary is very simple and only has a Routing Service that writes the message to an output folder.

 

Testing the sample

Once the schemas, map and orchestration is deployed to BizTalk, the itinerary is deployed to the Itinerary Store database and de custom component is placed in the GAC, the solution is ready to be tested. Create a simple Receive Port in the Administration Console and drop a SalesOrder message in it. I’ve used the Trace class in System.Diagnostics to trace the steps but you can also use another component for it like ETW tracing.

Run DebugView to watch the trace output.
 
The Itinerary picks up the event message and writes it to an output folder.

 

Conclusion

It took me quite some time to figure out which objects are necessary for the ESB Toolkit and how they work, but as always, once that is done, it is not very difficult to create the code and get it working!

You can download the Solution with the .XSD schemas, Map, Orchestration, Custom Component and Itinerary here:

Note
The sample is in BizTalk 2013 but this is also possible in BizTalk 2010

Tricks for Deleting Windows Azure Virtual Machines via the REST API

While I call this post “tricks” for deleting a Windows Azure Virtual Machine it really is not a trick.  It does require a little deeper understanding of how Windows Azure creates and hosts Virtual Machines in order to use the REST API to delete them.

Let us start with some background.  When you use the Windows Azure Portal to create a new Virtual Machine, a hidden service is created for you that will host the virtual machine role.  This will allow more than one Role (i.e. Virtual Machine) to be connected to this service to allow for the automatic round-robin of calls made to the exposed ports.

Below is a list of the Services in one of my Azure Account.

Using the REST API to list all Services, I see that not all of the services returned are shown on the screen in the portal.  The service named Test0206D is not listed inside the portal.

  <HostedService>
    <Url>https://management.core.windows.net/<subscription-id>/services/hostedservices/Test0206D</Url>
    <ServiceName>Test0206D</ServiceName>
    <HostedServiceProperties>
      <Description>Implicitly created hosted service2013-02-07 01:20</Description>
      <Location>West US</Location>
      <Label>VGVzdDAyMqZE</Label>
      <Status>Created</Status>
      <DateCreated>2013-02-07T01:20:58Z</DateCreated>
      <DateLastModified>2013-02-07T01:21:32Z</DateLastModified>
      <ExtendedProperties />
    </HostedServiceProperties>
  </HostedService>

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

In this case the Description clearly tells us this was created for us and when.  When we delete a Virtual Machine, the service will start to show up inside the portal.  If you never want to deploy a new Virtual Machine to that service again, it can be deleted.  Otherwise, keeping the service allows the DNS Name to be reserved and allows a new Virtual Machine Role to be added to this service at a later point. 

In order to Add a new Role (i.e. Virtual Machine) to an existing service, use the From Gallery option to create the Virtual Machine. Then, on step 4, Virtual Machine mode, select the Connect To An Existing Virtual Machine option. You will get a drop down of available options.

This background leads to the reason it might be tricky to delete a Virtual Machine using the REST API.

To Delete a stand-alone Virtual Machine

In an earlier blog post I listed the way to delete a Virtual Machine was to do an HTTP DELETE to the following URL.

https://management.core.windows.net/<Subscription-ID>/services/hostedservices/<Service-Name>/deploymentslots/Production

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

While this work, it only works in the case of a strand-alone Virtual Machine.  If you do not know if the Virtual Machine is stand-along or attached, do an HTTP GET to the URL above.  If you only have one <Role> element than it is a stand-alone Virtual Machine.

To Delete a Virtual Machine that is attached to another Virtual Machine

In order to Delete a Virtual Machine that is part of another service, you need to Remove the PersistentVMRole.  This is done by an HTTP DELETE to the following URL.

https://management.core.windows.net/<subscription-id>/services/hostedservices/<service-name>/deployments/<deployment-name>/roles/<vm-name>

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

The service name and virtual machine name are straight forward but the deployment name is tricky.  The deployment name is usually the Service Name but it can also be the name of the first virtual machine you put into the service.  To find out for sure, do an HTTP GET to the following URL to get the details of the service deployment.

https://management.core.windows.net/<subscription-id>/services/hostedservices/<service-name>/deploymentslots/Production

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

This will return an XML response that looks like the following.

<Deployment xmlns="http://schemas.microsoft.com/windowsazure" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <Name>Test0202A</Name>
  <DeploymentSlot>Production</DeploymentSlot>
  ...removed...
</Deployment>

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

The Name element is the name of the Deployment. 

In summary, a single call can be made to delete a stand-alone Virtual Machine.  For scenarios that join several Virtual Machines together inside a single service it takes a deeper understanding of that service’s properties in order to remove a Virtual Machine from that service.

Looking for a simple tool to work with the Windows Azure REST APIs?  I have a free tool here that greatly simplifies making REST calls.

SP2013: Adding an Embedded Yammer Feed to SharePoint

While setting up a new SharePoint solution one of the requirements was to embed a
Yammer Feed.

So I followed the help document only to have an unusual situation:

1) the feed wouldnt render in browse mode.

2) the feed WOULD render in Edit Mode.

The key to the situation was to declare a little more on the sample script Yammer
gave – adding ’text/javascript’

Here’s the working script – (one for the bat -utility belt)

*tested from Win8, Firefox *

<script data-app-id=’hyB2pTvrL36Y50py8EWj6A’ type=’text/javascript’ src=’https://assets.yammer.com/platform/yam.js’></script>
<script type=’text/javascript’>
yam.connect.embedFeed(
{ container: ‘#embedded-feed’
, network: ‘yournetworkhere’

});
</script>
<div id=’embedded-feed’></div>

Blog Post by: Mick Badran

MVC4 Makes it Easier to Create Desktop and Mobile Web Applications

Last year I wrote an article that expressed the importance of developing not only desktop solutions but mobile ones as well.  Since then it has become increasingly important to present not only to a desktop market, but to a mobile one as well.
That said, this article isn’t about convincing you to move to a desktop-mobile […]
Blog Post by: Karl Schwirz