Share via


Querying and Deleting 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.]

When deleting an entity it must be in scope. This is done by creating the Scope object and providing the AuthorityId, the ContainerId (that contains the entity), and the EntityId properties.

using (SitkaSoapServiceClient proxy = new SitkaSoapServiceClient("EndPointName"))
{
Scope myEntityScope = new Scope();
myEntityScope.AuthorityId = yourAuthorityId;
myEntityScope.ContainerId = yourContainerId;
myEntityScope.EntityId = yourEntityId;
// Delete entity
proxy.Delete(myEntityScope);
}

The following example first creates a sample book entity and then deletes it. To illustrate simple queries, after creating a sample entity, the example queries for the entity by using the Title. The entity id of the first book found with the specific title is then deleted. To create a working sample,

  • Add a service reference. For information on adding service reference, see Examples of Using SOAP Interface with the SQL Data Services.
  • Update code and provide existing authority id and new container id.
  • Provide credentials (user name, password) when application executes.
  • In the code below, the "using DeleteEntityUsingSOAP.ssdsClient" statement has two parts:
    • The first part (DeleteEntityUsingSOAP) 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 DeleteEntityUsingSOAP.ssdsClient;
using System.Collections.Generic;
using System;
using System.ServiceModel;
using System.Text;

namespace DeleteEntityUsingSOAP
{
    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 sampleEntityId = "<SampleEntityIdToCreateForDeletion>";


        private static SitkaSoapServiceClient proxy;

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

            string entityIdToDelete = "";

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

                // Set scope to the container where sample entity is 
                // added (for later deletion)
                Scope myContainerScope = new Scope();
                myContainerScope.AuthorityId = authorityId;
                myContainerScope.ContainerId = containerId;

                try
                {
                    // Step 1: First create a sample entity in the "UsedBooks" container.
                    CreateEntity(myContainerScope);

                    // Step 2: Delete the entity created in step 1. 
                    // You could easily set the scope using your entity id.
                    // But just to explore queries, the example first queries for the
                    // book by its title. Then uses the entity id of the first book found 
                    // with the specific title.
                    string queryString = @"from e in entities where e[""Title""] == ""My Test Book To Delete"" select e";
                    List<Entity> entities = proxy.Query(myContainerScope, queryString);
                    // If there are more, we delete only the 1st one found.
                    entityIdToDelete = entities[0].Id;

                    // First set scope to the specific entity
                    Scope myEntityScope = new Scope();
                    myEntityScope.AuthorityId = myContainerScope.AuthorityId;
                    myEntityScope.ContainerId = myContainerScope.ContainerId;
                    myEntityScope.EntityId = entityIdToDelete;

                    // Now delete
                    proxy.Delete(myEntityScope);

                    Console.WriteLine("Entity {0} deleted!", sampleEntityId);
                }
                catch (FaultException<Error> e)
                {
                    Console.WriteLine(e.Message);
                }
                catch (CommunicationException e)
                {
                    // Use this to echo out invalid credentials case.
                    Console.WriteLine("Unexpected error: {0}!", e.Message);
                }
            }
        }

        static void CreateEntity(Scope myContainerScope)
        {
            // Add a book
            Entity e1 = new Entity();
            e1.Id = sampleEntityId;
            // Set optional metadata property
            e1.Kind = "UsedBookKind";

            // Set flex properties
            e1.Properties = new Dictionary<string, object>();
            e1.Properties["Title"] = "My Test Book To Delete";
            e1.Properties["ISBN"] = "1-57880-066-3";
            e1.Properties["Author"] = "Mr. Author";
            e1.Properties["Publisher"] = "Mr. Publisher";
            e1.Properties["InPrint"] = true;
            e1.Properties["NumberOfCopiesSold"] = 5002m;
            e1.Properties["PublicationDate"] = DateTime.Parse("01/27/2007");
            e1.Properties["CoverPhoto"] = new byte[] { 0x1, 0x2, 0x3 }; // replace with real binary data

            proxy.Create(myContainerScope, e1);
            Console.WriteLine("Entity {0} created!", e1.Id);
        }

        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 that the entity was deleted, specify the entity URI in the browser and you will get appropriate "not found" error.

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

You can specify an empty query with the specific container in the scope to retrieve all the entities in that container. For details about queries, see Querying SQL Data Services.

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

See Also

Concepts

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