Profile MetaDataException

on Friday, July 17, 2009

If you get an error of the following type:


An exception of type 'Microsoft.Commerce.Providers.Metadata.MetadataException'
occurred and was
caught.
---------------------------------------------------------------------------
07/17/2009
10:46:57
Type : Microsoft.Commerce.Providers.Metadata.MetadataException,
Microsoft.Commerce.Providers, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35
Message : Commerce Server class
'Microsoft.CommerceServer.Runtime.Profiles.Profile' or its definition 'Something'
is not found in the Commerce Server metadata.
Source :
Microsoft.Commerce.Providers
Help link :
Data :
System.Collections.ListDictionaryInternal
TargetSite :
Microsoft.Commerce.Providers.Metadata.CommerceServerEntity
GetCommerceServerEntity(System.String, System.String)
Stack Trace : at
Microsoft.Commerce.Providers.Metadata.MergedMetadata.GetCommerceServerEntity(String
commerceClassName, String commerceDefinitionName)
at
Microsoft.Commerce.Providers.Metadata.MergedMetadata.CreateMergedMetadata(CommerceEntityDelta
repositoryСommerceEntityDelta, EntityMappingDelta repositoryEntityMapping)
at
Microsoft.Commerce.Providers.Metadata.MergedMetadata.MergeMetadata(InheritableCollection`1
repositoryMetadata, MetadataCollection`1 commerceServerMetadata)
at
Microsoft.Commerce.Providers.Metadata.MergedMetadataLoader.GetMergedMetadata(String
siteChannelKey, OperationCacheDictionary operationCache)
at
Microsoft.Commerce.Providers.Metadata.MergedMetadataLoader.ExecuteQuery(CommerceQueryOperation
queryOperation, OperationCacheDictionary operationCache,
CommerceQueryOperationResponse response)
at
Microsoft.Commerce.Providers.Components.OperationSequenceComponent.Execute(CommerceOperation
operation, OperationCacheDictionary operationCache, CommerceOperationResponse
response)
at
Microsoft.Commerce.Broker.OperationSequence.ExecuteComponentTree(List`1
executionTreeList, CommerceOperation operation, OperationCacheDictionary
operationCache, CommerceOperationResponse response)
at
Microsoft.Commerce.Broker.OperationSequence.Execute(CommerceOperation
operation)
at Microsoft.Commerce.Broker.MessageHandler.ProcessMessage(String
messageHandlerName, CommerceOperation operation)
at
Microsoft.Commerce.Broker.OperationService.InternalProcessRequest(CommerceRequest
request)
at
Microsoft.Commerce.Providers.Utility.CommerceEntityMetadata.ExecuteMetadataQuery(MetadataCacheKey
cacheKey, String modelName)
at
Microsoft.Commerce.Application.Common.CachedFactory`2.GetOrCreate(TKey key,
CreateInstance`2 factory)
at
Microsoft.Commerce.Providers.Utility.CommerceEntityMetadata.Get(String
modelName, Nullable`1 commerceArea)
at
Microsoft.Commerce.Providers.Utility.ProfileMetadata.<>c__DisplayClass5.b__4(MetadataCacheKey
key)
at
Microsoft.Commerce.Application.Common.CachedFactory`2.GetOrCreate(TKey key,
CreateInstance`2 factory)
at
Microsoft.Commerce.Providers.Utility.ProfileMetadata.Get(String modelName)
at
Microsoft.Commerce.Providers.Components.ProfileOperationSequenceComponent.get_Metadata()
at
Microsoft.Commerce.Providers.Components.ProfileOperationSequenceComponent.AreSearchParametersValid(CommercePropertyCollection
properties)
at
Microsoft.Commerce.Providers.Components.ProfileOperationSequenceComponent.ValidateSearchCriteria(CommerceModelSearch
searchCriteria)
at
Microsoft.Commerce.Providers.Components.ProfileOperationSequenceComponent.GetMatches(CommerceModelSearch
searchCriteria, Nullable`1& totalItemCount, Boolean throwIfNotFound)
at
Microsoft.Commerce.Providers.Components.ProfileLoaderBase.ExecuteQuery(CommerceQueryOperation
queryOperation, OperationCacheDictionary operationCache,
CommerceQueryOperationResponse response)
at
Microsoft.Commerce.Providers.Components.OperationSequenceComponent.Execute(CommerceOperation
operation, OperationCacheDictionary operationCache, CommerceOperationResponse
response)
at
Microsoft.Commerce.Providers.Components.ProfileOperationSequenceComponent.Execute(CommerceOperation
operation, OperationCacheDictionary operationCache, CommerceOperationResponse
response)
at
Microsoft.Commerce.Broker.OperationSequence.ExecuteComponentTree(List`1
executionTreeList, CommerceOperation operation, OperationCacheDictionary
operationCache, CommerceOperationResponse response)
at
Microsoft.Commerce.Broker.OperationSequence.Execute(CommerceOperation
operation)
at Microsoft.Commerce.Broker.MessageHandler.ProcessMessage(String
messageHandlerName, CommerceOperation operation)
at
Microsoft.Commerce.Broker.OperationService.InternalProcessRequest(CommerceRequest
request)
at
Microsoft.Commerce.Broker.OperationService.ProcessRequest(CommerceRequest
request)
A couple of things you may want to check are:

1. Make sure the entity definition inside MetaDataDefinitions.xml is correct.
3. Verify the entries into the Channel Config.
2. If you have extended the Profile entity make sure that the changes you have made to the Profile Schema don't have any name mismatches. So open up Commerce Server Manager and make sure that the Profile Definition and the Data Object is named appropriately.

Creating and Accessing Site Terms through Mojave API

on Thursday, January 1, 2009

The purpose of this entry is to demonstrate how to access site terms through Mojave API.

What is a Site Term?

A site term is a set of valid values for information that the user provides. For example, if you added a custom property named "Gender" to the profile named "User," you could define a site term to represent the values "male" and "female," which would provide the valid values for the "Gender" property [1].

How do you Create a Site Term?

There are several ways that you can create the site terms including direct user interaction with the Microsoft Commerce Server Customer and Orders management application. To do this, after opening the Customer And Orders management application,

  • Click on "Site Terms" in the Views pane on the left hand side.
  • Click "Create New Site Term" in the Tasks pane.
  • Fill in the information in the window that pops up to create the new site terms. In the following screenshot we are creating a site term called Security Questions which will be used to hold the security questions that will be presented to the user upon account creation.

  • Click "Save and Close" and you will have created a new site term in Commerce Server 2007.
If you wish, you could also create site terms through code. The purpose of this entry is to show you how you can bubble up existing site terms in your code through Mojave, so we will leave creating site terms through code for another time.

Accessing Site Terms through Mojave API

So now that you have a site term, the next thing you want to do with it is to have access to it in your code. So you can actually reach into the Commerce Server boundary, extract this particular site term and show it in your UI, e.g. extract the security questions from the site terms and display it on your user registration page.

If you are working with the default website that ships with the current Mojave download (CTP4, CTP5), one of the first things you need to do is to add a enumeration entry for your newly created site term. Inside the Common/DateItems folder you will find enums.cs which contains the definition for an enum called SiteTermName. This is simply an enumeration of the site terms that you have available inside Commerce Server. You can add your newly created site term to the bottom of this enum, and make sure that you spell it correctly as you did in the Commerce Server Customers and Orders management application.


SecurityQuestions = 7


The next thing you need to identify is that inside the ProfileController class, there is a method called GetSiteTerm, which takes an SiteTermName enum as a parameter, and returns a CommerceRelationshipList of the specific site term. By inspecting the code closely, you will see that this is done by creating a Commerce Query on the SiteTerm model, and passing in the Site Term Name as the model ID. This returns the entire contents of the site terms as a list.


public static CommerceRelationshipList GetSiteTerm(SiteTermName siteTermName)
{
CommerceRelationshipList siteTerms = new CommerceRelationshipList();
CommerceQueryRelatedItem elementQuery = new CommerceQueryRelatedItem(SiteTerm.RelationshipName.Elements);
elementQuery.Model.Properties.Add(SiteTermElement.PropertyName.Id);
elementQuery.Model.Properties.Add(SiteTermElement.PropertyName.DisplayName);
CommerceQuery query = new CommerceQuery();
query.SearchCriteria.Model.Id = siteTermName.ToString();
query.Model.Properties.Add(SiteTerm.PropertyName.Id);
query.RelatedOperations.Add(elementQuery);
CommerceResponse response = SiteContext.ProcessRequest(query.ToRequest());
CommerceQueryOperationResponse queryResponse = response.OperationResponses[0] as CommerceQueryOperationResponse;
SiteTerm st = queryResponse.CommerceEntities[0];
if (st != null)
{
siteTerms = st.Elements;
}
return siteTerms;
}


So now you have a method that reaches inside the Commerce Server and grabs the Site Term you are interested in. All that remains is a call to this method. You may want to do this from say a helper class or the presenter itself. Here is a function that returns the Security Question site term contents:


public static CommerceRelationshipList GetSecurityQuestions()
{
return ProfileController.GetSiteTerm(SiteTermName.SecurityQuestions);
}


If you like you can convert this CommerceRelationshipList into a keyed collection as you wish.

References:

1. What is a site term?

Debugging Sharepoint Applications in IIS7

on Friday, December 19, 2008

We all know how to debug classic ASP.NET application using the famous "Attach to Process" tool in our favourite IDE. However, when debugging sharepoint applications, you need to attach to the w3wp.exe thread. You will notice that you may have multiple w3wp.exe thread showing up in your Attach to Process window.

How do you know which process to attach to. One dirty and long way to do is to set a breakpoint, and then attach each process one by one. If the breakpoint is a solid red, then you know that you got the right process. However, this is not the recommended way as there may be other reason why you may get a hollow red breakpoint icon. The symbols may not have been loaded yet, you may have missed a dll or two while GAC'ing between builds, etc.

A better way to pinpoint which w3wp.exe process to attach to, use the following dos command:

%windir%\system32\inetsrv\appcmd.exe list wp

This is what the output would look like on a console:


You can even throw this inside a batch file which gets called at the end of the a post-build event.

Note that this is only applicable for servers running IIS7. Previous IIS versions had a different tool called iisapp.vbs which is no longer available with IIS7.

Sorting Profile in Commerce Server 2007 and Mojave

At the time of this writing sorting is not supported by Mojave on the Profile entity. As a matter of fact, if you write and execute a Mojave query, the sortProperties attribute will only have any effect if you are dealing with one of the following entities:

  • Catalog
  • CatalogEntity
  • Category
  • Product
  • Variant
The CommerceSortProperty will simply be ignored for any other entities including Profile.

So how exactly do you sort one of these entities? Well, not surprisingly, you will have to do it yourself. For example, if you run a query to get the credit cards from the user profile, the returned list will be random at best. Typically, you will store this result inside a Collection object of some sort. The DefaultSite that ships with CTP4, uses a generic Collection object called Collection. Before you return this unsorted list to your caller, you can send this object to the following method which will sort it for you.

private static ICollection SortCreditCardsCollectionByDateCreated(Collection creditCardsCollection)
{
List listCreditCards = new List(creditCardsCollection);
listCreditCards.Sort(
delegate(CreditCard x, CreditCard y)
{ return DateTime.Compare(x.DateCreated, y.DateCreated); }
);
return (new Collection(listCreditCards));
}

The above method sorts the list on the DateCreated attribute. You can take it one step further and generalize this method to actually pass in the attribute on which you wish to sort, and place it in a MojaveUtilities class if you wish.

And now, you can sort profile or any other entity you wish to sort in Commerce Server 2007 using Mojave API.

Refreshing Cache from Mojave API

My last post was regarding how to refresh CS07 cache concerning the web services. It is important to note however that when you are dealing with Mojave code, such as working on the Default Site that ships with CTP4 (or upcoming CTP5), that the code does not use these web services. These web services are used by integration applications (e.g. Biztalk) or the management application MMCs (e.g. Customers and Orders Manager, Catalog Manager, etc.).

One example would be when you a user adds a credit card to his profile. A new credit card entry is created which includes all the information e.g. credit card number, expiry date, etc. However, it does not include the DateCreated i.e. the time stamp at which the user created the credit card entry on the site. This attribute is filled in on the Commerce Server/Sql Server side. Therefore your local cached copy of the newly created credit card will not have DateCreated attribute in the cache. Why is this a problem? Well, if you want to sort the credit cards by DateCreated attribute, this is a problem. You will not be able to do a meaningful sort until and unless the CS07 cache refreshes. An iisreset, for example, will clear the cache for you. Note that resetting the cache from the management applications or changing the settings in the Commerce Server web services' web.config will have no effect. Remember, you are making Mojave calls which are going straight to CS07 (Microsoft.CommerceServer.* libraries). The web services are simply not in the play here.

So, how do you refresh Commerce Server 2007 cache from Mojave API? There are two ways:

1. Directly Using CommerceUpdate

CommerceUpdate updateCache = new CommerceUpdate();
updateCache.SearchCriteria.Model.Name = "ProfileCache";

CommerceResponse response = OperationService.ProcessRequest(GetCurrentRequestContext(), updateCache.ToRequest());

2. When a CommerceQuery is done. If the search criteria contains the last modified date, it will be evaluated against the retrieved user profile and if different, the CS2007 cache will be refreshed.

CommerceQuery query = new CommerceQuery();
query.SearchCriteria.Model.DateModified = DateTime.UtcNow;
query.SearchCriteria.Model.Email = email;

Refreshing Cache in Commerce Server 2007

on Wednesday, December 17, 2008

Commerce Server heavily reiles on caching to keep the application flowing nice and tidy. During development, there may be cases where you want the application to go and fetch data right from the source and bypass the cache. There are a couple of things you can do:

  1. From the management applications, e.g. Commerce Server Customers and Order manager, you can click on the Profiles or Payment Methods, and then click "Refresh Site Cache" from the task pane.Commerce Server - Refresh Cache

  2. You can browse to the following URL: http://webserver:webservicesPort/OrdersWebService/SiteCacheRefresh.axd?CacheToRefresh=ProfileCache (replace ProfileCache with whichever cache you are trying to refresh).

  3. If you get an access denied error, then you need to add the owner of your application pool to the appropriate section in the web.config of the web services. Look for "SiteCacheRefresh.axd" in your web service's web.config and it will have "". In that section allow access to your application pool owner, or (for development box only) change "deny" to "allow" in the above tag giving everyone free access.
Here is a great link on How to Control the Profile Cache in Commerce Server: http://blogs.msdn.com/maxakbar/archive/2007/02/07/how-to-control-the-profile-cache.aspx

Tree-To-Flat Copy Using XCopy

on Tuesday, December 16, 2008

If you have a tree folder as follows:

%BRANCHPATH%\UserControls
%BRANCHPATH%\UserControls\ChangePassword\
%BRANCHPATH%\UserControls\MyProfile\
%BRANCHPATH%\UserControls\RegistrationWizard\


and you try to do a XCOPY as follows using the recursive option (/S), it copies entire folder structure into the target hive.

XCOPY %BRANCHPATH%\*.ascx "%HIVE%" /Y /R /S

However, if we just want to drop the files individually the above doesn’t work. The above will copy entire folders that contain the ascx files in the HIVE. This will cause problems. There is no way to do a tree-to-flat copy using any XCopy option.

One easy but cumbersome way to avoid this is to use full paths to each folder as follows:

XCOPY %BRANCHPATH%\UserControls\*.ascx "%HIVE%" /Y /R
XCOPY %BRANCHPATH%\UserControls\ChangePassword\*.ascx "%HIVE%" /Y /R
XCOPY %BRANCHPATH%\UserControls\MyProfile\*.ascx "%HIVE%" /Y /R
XCOPY %BRANCHPATH%\UserControls\RegistrationWizard\*.ascx "%HIVE%" /Y /R


The above will work, but you will have to specify each folder, and there may be a folder you end up missing. However, the following snippet is generic enough in that it will first create a list of all the folders and subfolders, create a temporary list of folders in a text file, go through each folder, grab the .ascx files in each folder and copy it to the HIVE. At the end delete the temporary text file:

dir %BRANCHPATH%\UserControls /A:D /B /S > tempListOfDirs.txt
For /F %%A IN (tempListOfDirs.txt) Do (
If Exist %%A* (
XCOPY %%A\*.ascx "%HIVE%" /Y /R
)
)
del tempListOfDirs.txt