Share via


Creating an Entity Using SOAP

[This document supports a preliminary release of a software product that may be changed substantially prior to final commercial release. This document is provided for informational purposes only.]

To create an entity a container must be in scope. This is done by creating the Scope object and then providing the AuthorityId and the ContainerId property values.

using (SitkaSoapServiceClient proxy = new  SitkaSoapServiceClient("EndPointName"))
{
proxy.ClientCredentials.UserName.UserName = userName;
proxy.ClientCredentials.UserName.Password = userPw;

Scope myContainerScope = new Scope();
myContainerScope.AuthorityId = YourAuthorityId;
myContainerScope.ContainerId = YourContainerId;

// Now create a new entity
Entity e1 = new Entity();
e1.Id = "SomeId";
e1.Kind = "UsedBookKind";
e1.Properties = new Dictionary<string, object>();
…
// Create
proxy.Create(myContainerScope, e1);
}

The following example creates a book entity in one of the existing containers. To create a working sample,

  • Add a service reference. For detail steps, see Examples of Using SOAP Interface with the SQL Data Services.
  • Update code and provide your existing authority id, container id and new entity id.
  • Provide credentials (user name, password) when application executes.
  • In the code below, the "using CreateEntityUsingSOAP.ssdsClient" statement has two parts:
    • The first part (CreateEntityUsingSOAP) is the namespace name. This namespace name must match the Default namespace of the application.
    • The second part (ssdsClient) is the name you provided when you added the service reference.
using CreateEntityUsingSOAP.ssdsClient;
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Text;

namespace CreateEntityUsingSOAP
{
    class Program
    {
        // Provide your own values for these member variables
        private static string userName;
        private static string password;
        private const string authorityId = "<YourExistingAuthorityId>";
        private const string containerId = "<YourExistingContainerId>";
        private const string entityId = "<EntityIdToCreate>";

        static void Main(string[] args)
        {
            Console.Write("Username: ");
            userName = Console.ReadLine();
            Console.Write("Password: ");
            password = ReadPassword();

            using (SitkaSoapServiceClient proxy = new SitkaSoapServiceClient("BasicAuthEndpoint"))
            {
                proxy.ClientCredentials.UserName.UserName = userName;
                proxy.ClientCredentials.UserName.Password = password;

                // Identify scope. To create an entity you need to specify container scope.
                Scope myContainerScope = new Scope();
                myContainerScope.AuthorityId = authorityId;
                myContainerScope.ContainerId = containerId;

                // Let us create a Book entity
                Entity e1 = new Entity();
                // Set requied metadata properties
                e1.Id = entityId;
                // Set optional metadata property
                e1.Kind = "UsedBookKind";
                // Set flexible properties
                e1.Properties = new Dictionary<string, object>();
                e1.Properties["Title"] = "My Book";
                e1.Properties["ISBN"] = "1-57880-066-36";
                e1.Properties["Author"] = "Mr. Author";
                e1.Properties["Publisher"] = "Mr. Publisher";
                e1.Properties["InPrint"] = false;
                e1.Properties["NumberOfCopiesSold"] = 250m; //decimal
                e1.Properties["PublicationDate"] = DateTime.Parse("01/27/2004");
                e1.Properties["CoverPhoto"] = new byte[] { 0x1, 0x2, 0x3 };    // replace with real binary data
                try
                {
                    proxy.Create(myContainerScope, e1);

                    Console.WriteLine("Entity {0} created!", e1.Id);
                }
                catch (FaultException<Error> e)
                {
                    Console.Write("Error: {0}", e.Message);
                }
                catch (CommunicationException e)
                {
                    Console.Write("Error: {0}", e.Message);
                }
            }
        }

        private static string ReadPassword()
        {
            StringBuilder retval = new StringBuilder();
            ConsoleKeyInfo keyInfo;
            while ((keyInfo = Console.ReadKey(true)).Key != ConsoleKey.Enter)
            {
                Console.Write("*");
                retval.Append(keyInfo.KeyChar);
            }
            Console.WriteLine();

            return retval.ToString();
        }
    }
}

To verify the result, enter the entity URI in the browser to retrieve the specific

entity:

https://<authority-id>.data.database.windows.net/v1/<container-id>/<entity-id>

Note

When querying for non-blob entities your browser need to be aware of the application/x-ssds+xml SDS content type. For Internet Explorer this can be done by adding a new key in the registry. For more information, see Guidelines and Limitations.

The sample book entity XML is shown below:

<UsedBookKind xmlns:s="https://schemas.microsoft.com/sitka/2008/03/" 
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
              xmlns:x="http://www.w3.org/2001/XMLSchema">
   <s:Id>Entity01</s:Id> 
   <s:Version>version-number</s:Version> 
   <Title xsi:type="x:string">My Book</Title> 
   <ISBN xsi:type="x:string">1-57880-066-36</ISBN> 
   <Author xsi:type="x:string">Mr. Author</Author> 
   <Publisher xsi:type="x:string">Mr. Publisher</Publisher> 
   <InPrint xsi:type="x:boolean">false</InPrint>
   <NumberOfCopiesSold xsi:type="x:decimal">250</NumberOfCopiesSold> 
   <PublicationDate xsi:type="x:dateTime">2004-01-   27T00:00:00</PublicationDate> 
   <CoverPhoto xsi:type="x:base64Binary">AQID</CoverPhoto> 
</UsedBookKind>

You may specify empty query with specific container in scope to find all the entities in that container:

https://<authority-id>.data.database.windows.net/v1/<container-id>?q='' 

For details about queries, see Querying SQL Data Services.

See Also

Concepts

Getting Ready to Use SQL Data Services
SDS Data Model Overview (Authorities, Containers, Entities and Flexible Entities)
Creating an Entity Using REST
Examples of Using SOAP and REST Interfaces with the SQL Data Services