Skip to main content

Code Snippets for UpmMembership, Authenticate,LastLockoutDate & CreateUser

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Microsoft.CommerceServer.Runtime;
using Microsoft.CommerceServer.Runtime.Profiles;

namespace ASPNet3346
{
    public partial class UpmMembership : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            UpmMembershipUser user = (UpmMembershipUser)Membership.GetUser("foo");
            //user.LastLockoutDate;
            //user.IsLockedOut;

        }

        protected void btnAuthenticate_Click(object sender, EventArgs e)
        {
            bool result=Membership.ValidateUser("rajendra@dewani.net", "rajendra@dewani.net");
            DisplayHelper.DisplayValue("ValidateUser ", result.ToString(), Response);
        }
        protected void btnTryLock_Click(object sender, EventArgs e)
        {
            bool result = Membership.ValidateUser("rajendra@dewani.net", "ABC");
            DisplayHelper.DisplayValue("ValidateUser ", result.ToString(), Response);
            MembershipUser user = Membership.GetUser("rajendra@dewani.net");
            if (user!=null)
            {
            DisplayHelper.DisplayValue("Is Locked  ", user.IsLockedOut, Response);
            if (user.LastLockoutDate!=null)
                DisplayHelper.DisplayValue(" Locked Date  ", user.LastLockoutDate.ToString("dd/MMM/yyyy hh:mm:ss"), Response);
            }
        }

        protected void btnCreateUser_Click(object sender, EventArgs e)
        {
            MembershipCreateStatus status;
            Membership.CreateUser("rajendra@dewani.net", "rajendra@dewani.net", "rajendra@dewani.net",
                                  "rajendra@dewani.net", "rajendra@dewani.net", true, null, out status);
            if (status==MembershipCreateStatus.Success)
            {
                DisplayHelper.DisplayValue("User Created", "rajendra@dewani.net", Response);
            }
        }
    }
}

Attached Source Code : ASPNet3346

How to get Child Products using Commerce Server 2007 & 2009

using System;
using System.Linq;
using Microsoft.Commerce.Common.MessageBuilders;
using Microsoft.Commerce.Contracts;
using Microsoft.Commerce.Contracts.Messages;
using Microsoft.CommerceServer.Catalog;
using Microsoft.CommerceServer.Runtime;

namespace ASPNet3346
{
    public partial class GetChildProducts : System.Web.UI.Page
    {
        protected void btnCS2007_Click(object sender, EventArgs e)
        {
            ProductCatalog productCatalog = (ProductCatalog)CommerceContext.Current.CatalogSystem.GetCatalog("Adventure Works Catalog", "en-US");
            // Get the root category.
            Category departmentCategory = productCatalog.GetCategory("Boots");
            ProductCollection childProducts = departmentCategory.ChildProducts;
            foreach (Product product in childProducts)
            {
                DisplayHelper.DisplayValue("DisplayName", product.DisplayName, Response);
            }
        }

        protected void btnCS2009_Click(object sender, EventArgs e)
        {

            var catalogQuery = new CommerceQuery("Category");
            catalogQuery.SearchCriteria.Model.Id = "Boots";
            catalogQuery.SearchCriteria.Model.Properties["CatalogId"] = "Adventure Works Catalog";

            //Get Child Categories
            // Add Related Query Operation for child products
            {
                CommerceQueryRelatedItem queryChildProducts =
                    new CommerceQueryRelatedItem("ChildProducts", "Product");
                catalogQuery.RelatedOperations.Add(queryChildProducts);
            }
            CommerceResponse response = CommerceFoundationServiceAgent.Execute(catalogQuery);

            // Handle Responses
            CommerceQueryOperationResponse queryResponse = response.OperationResponses.Single() as CommerceQueryOperationResponse;
            CommerceEntity productCatalog = queryResponse.CommerceEntities.SingleOrDefault();
            var childCategories = productCatalog.GetPropertyValue("ChildProducts") as CommerceRelationshipList;
            if (childCategories != null)
                foreach (var commerceRelationship in childCategories)
                {
                    CommerceEntity childcategory = commerceRelationship.Target;
                    DisplayHelper.DisplayValue("DisplayName", childcategory.Properties["DisplayName"], Response);
                }
        }
    }
}

Attached Source Code : ASPNet3346

How to get Child Categories using Commerce Server 2007 & 2009

using System;
using System.Linq;
using Microsoft.Commerce.Common.MessageBuilders;
using Microsoft.Commerce.Contracts;
using Microsoft.Commerce.Contracts.Messages;
using Microsoft.CommerceServer.Catalog;
using Microsoft.CommerceServer.Runtime;

namespace ASPNet3346
{
    public partial class GetChildCategories : System.Web.UI.Page
    {
        protected void btnCS2007_Click(object sender, EventArgs e)
        {
            ProductCatalog productCatalog = (ProductCatalog)CommerceContext.Current.CatalogSystem.GetCatalog("Adventure Works Catalog", "en-US");
            // Get the root category.
            Category departmentCategory = productCatalog.GetCategory("Departments");
            CatalogItemsDataSet categories = departmentCategory.ChildCategories.DataSet;
            foreach (CatalogItemsDataSet.CatalogItem category in categories.CatalogItems)
            {
                DisplayHelper.DisplayValue("DisplayName", category.DisplayName, Response);
            }

        }

        protected void btnCS2009_Click(object sender, EventArgs e)
        {

            var catalogQuery = new CommerceQuery("Category");
            catalogQuery.SearchCriteria.Model.Id = "Departments";
            catalogQuery.SearchCriteria.Model.Properties["CatalogId"] = "Adventure Works Catalog";

            //Get Child Categories
            // Add Related Query Operation for child products
            {
                CommerceQueryRelatedItem queryChildProducts =
                    new CommerceQueryRelatedItem("ChildCategories", "Category");
                //the relation name can ChildCategories,AncestorCategories,CanonicalCategories,ParentCategories,RelatedCategories , Ref MetadataDefinitions.xml
                catalogQuery.RelatedOperations.Add(queryChildProducts);
            }
            CommerceResponse response = CommerceFoundationServiceAgent.Execute(catalogQuery);

            // Handle Responses
            CommerceQueryOperationResponse queryResponse = response.OperationResponses.Single() as CommerceQueryOperationResponse;
            CommerceEntity productCatalog = queryResponse.CommerceEntities.SingleOrDefault();
            var childCategories = productCatalog.GetPropertyValue("ChildCategories") as CommerceRelationshipList;
            foreach (var commerceRelationship in childCategories)
            {
                CommerceEntity childcategory = commerceRelationship.Target;
                DisplayHelper.DisplayValue("DisplayName", childcategory.Properties["DisplayName"], Response);
            }

        }
    }
}

Attached Source Code : ASPNet3346

How to get Root Categories using Commerce Server 2007 & 2009

using System;
using System.Linq;
using Microsoft.Commerce.Common.MessageBuilders;
using Microsoft.Commerce.Contracts;
using Microsoft.Commerce.Contracts.Messages;
using Microsoft.CommerceServer.Catalog;
using Microsoft.CommerceServer.Runtime;

namespace ASPNet3346
{
    public partial class GetCatalogCategories : System.Web.UI.Page
    {
        protected void btnCS2007_Click(object sender, EventArgs e)
        {
            ProductCatalog productCatalog = (ProductCatalog)CommerceContext.Current.CatalogSystem.GetCatalog("Adventure Works Catalog", "en-US");
            // Get the root category.
            Category rootCategory = productCatalog.GetRootCategory();
            CatalogItemsDataSet categories = rootCategory.ChildCategories.DataSet;
            foreach (CatalogItemsDataSet.CatalogItem category in categories.CatalogItems)
            {
                DisplayHelper.DisplayValue("DisplayName",category.DisplayName,Response);
            }

        }

        private void GetRootCategory()

        {

            var catalogQuery = new CommerceQuery("Catalog");

            //catalogQuery.SearchCriteria.Model.Properties["language"] = "en-US";
            catalogQuery.SearchCriteria.Model.Id = "Adventure Works Catalog";

            //Get Root Category
            // Add Related Query Operation for root Category ie. _root_
            {
                CommerceQueryRelatedItem queryRootCategory =
                    new CommerceQueryRelatedItem("RootCategory", "Category");
                queryRootCategory.Model.Properties.Add("Id");
                queryRootCategory.Model.Properties.Add("DisplayName");
                catalogQuery.RelatedOperations.Add(queryRootCategory);
            }

            CommerceResponse response = CommerceFoundationServiceAgent.Execute(catalogQuery);

            // Handle Responses
            CommerceQueryOperationResponse queryResponse = response.OperationResponses.Single() as CommerceQueryOperationResponse;
            CommerceEntity productCatalog = queryResponse.CommerceEntities.SingleOrDefault();

            DisplayHelper.DisplayValue("Catalog ID", productCatalog.Id, Response);
            DisplayHelper.DisplayValue("IdentifyingProductProperty", productCatalog.Properties["IdentifyingProductProperty"], Response);

            var rootCategory = (productCatalog.GetPropertyValue("RootCategory") as CommerceRelationship).Target;

            DisplayHelper.DisplayValue("Category ID", rootCategory.Id, Response);
            DisplayHelper.DisplayValue("Category DisplayName", rootCategory.Properties["DisplayName"].ToString(), Response);

        }

        protected void btnCS2009_Click(object sender, EventArgs e)
        {
            // GetRootCategory(); // Optional for your knowledge

            var catalogQuery = new CommerceQuery("Category");

            catalogQuery.SearchCriteria.Model.Properties["DisplayName"] = "_root_";//this indicates its root category, Please refer GetRootCategory() for more details
            catalogQuery.SearchCriteria.Model.Properties["CatalogId"] = "Adventure Works Catalog";

            //Get Child Categories
            // Add Related Query Operation for child products
            {
                CommerceQueryRelatedItem queryChildProducts =
                    new CommerceQueryRelatedItem("ChildCategories", "Category");

                catalogQuery.RelatedOperations.Add(queryChildProducts);
            }
            CommerceResponse response = CommerceFoundationServiceAgent.Execute(catalogQuery);

            // Handle Responses
            CommerceQueryOperationResponse queryResponse = response.OperationResponses.Single() as CommerceQueryOperationResponse;
            CommerceEntity productCatalog = queryResponse.CommerceEntities.SingleOrDefault();
            var childCategories = productCatalog.GetPropertyValue("ChildCategories") as CommerceRelationshipList;
            foreach (var commerceRelationship in childCategories)
            {
                CommerceEntity childcategory = commerceRelationship.Target;
                DisplayHelper.DisplayValue("Root Category ID", childcategory.Id, Response);
            }

        }
    }
}

Attached Source Code : ASPNet3346

How to get product using Commerce Server 2007 & 2009

using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Microsoft.Commerce.Common.MessageBuilders;
using Microsoft.Commerce.Contracts;
using Microsoft.Commerce.Contracts.Messages;
using Microsoft.CommerceServer.Catalog;
using Microsoft.CommerceServer.Runtime;

namespace ASPNet3346
{
    public partial class GetAProduct : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnCS2007_Click(object sender, EventArgs e)
        {
            GetCatalog(CommerceContext.Current.CatalogSystem, "Adventure Works Catalog");
        }
        private void GetCatalog(CatalogContext context, string catalogName)
        {
            //Get the catalog with the name specified in catalogName.
            // Return the catalog data in English, as specified by "en-US");
            ProductCatalog productCatalog = (ProductCatalog)context.GetCatalog(catalogName, "en-US");
            Response.Write(productCatalog.Name);
            Response.Write(productCatalog.IdentifyingProductProperty);
        }

        protected void btnCS2009_Click(object sender, EventArgs e)
        {
            var productQuery = new CommerceQuery("Product");

            productQuery.SearchCriteria.Model.Properties["CatalogId"] = "Adventure Works Catalog";
            productQuery.SearchCriteria.Model.Id = "AW200-12";

            productQuery.Model.Properties.Add("Id");
            productQuery.Model.Properties.Add("DisplayName");
            productQuery.Model.Properties.Add("Description");
            productQuery.Model.Properties.Add("ListPrice");

            {
                var queryVariants = new CommerceQueryRelatedItem("Variants", "Variant");

                queryVariants.SearchCriteria.SortProperties = new List();
                queryVariants.SearchCriteria.SortProperties.Add(new CommerceSortProperty("Variant", "ProductColor", SortDirection.Ascending));
                queryVariants.SearchCriteria.FirstItemIndex = 0;
                queryVariants.SearchCriteria.ReturnTotalItemCount = true;

                queryVariants.Model.Properties.Add("Id");
                queryVariants.Model.Properties.Add("VariantCode");
                queryVariants.Model.Properties.Add("ProductColor");

                productQuery.RelatedOperations.Add(queryVariants);
            }

            CommerceResponse response = CommerceFoundationServiceAgent.Execute(productQuery);

            // Handle Responses
            CommerceQueryOperationResponse queryResponse = response.OperationResponses.Single() as CommerceQueryOperationResponse;
            CommerceEntity product = queryResponse.CommerceEntities.SingleOrDefault();
            Response.Write(product.Id);
        }
    }
}

Attached Source Code : ASPNet3346

How to get a Catalog using Commerce Server 2007 & 2009

namespace ASPNet3346
{
    public partial class GetACatalog : System.Web.UI.Page
    {

        protected void btnCS2007_Click(object sender, EventArgs e)
        {

            ProductCatalog productCatalog = (ProductCatalog)CommerceContext.Current.CatalogSystem.GetCatalog("Adventure Works Catalog", "en-US");
            DisplayHelper.DisplayValue("Catalog ID", productCatalog.Name, Response);
            DisplayHelper.DisplayValue("IdentifyingProductProperty", productCatalog.IdentifyingProductProperty, Response);

        }

        protected void btnCS2009_Click(object sender, EventArgs e)
        {
            var catalogQuery = new CommerceQuery("Catalog");

            //TODO Theme Microsoft Multi-Channel Commerce Foundation RequestContext object will BindingContainert he user locale information used to get the catalogs in the appropriate language. If the language in the RequestContext is NotFiniteNumberExceptionc urrently supported, the _Defaultl anguage for thec atalog(s) willb e used.
            //catalogQuery.SearchCriteria.Model.Properties["language"] = "en-US";
            catalogQuery.SearchCriteria.Model.Id = "Adventure Works Catalog";

            CommerceResponse response = CommerceFoundationServiceAgent.Execute(catalogQuery);

            // Handle Responses
            CommerceQueryOperationResponse queryResponse = response.OperationResponses.Single() as CommerceQueryOperationResponse;
            CommerceEntity productCatalog = queryResponse.CommerceEntities.SingleOrDefault();

            DisplayHelper.DisplayValue("Catalog ID", productCatalog.Id, Response);
            DisplayHelper.DisplayValue("IdentifyingProductProperty", productCatalog.Properties["IdentifyingProductProperty"], Response);

        }

    }
}

Attached Source Code : ASPNet3346