Metadata Execute 401 Unauthorized error

Question Status

nfjeld asked a question on 7 Jul 2008 6:49 AM


I have been using the CRM 4.0 SDK to create a custom website.  I have been able to do pretty much everything I want.

I created a method for creating a crm service and metadata service that I use throught my application.  I can read and create contacts and custom entitiesjust fine.  I can also use the metadata service to retrieve picklist attribute values just fine.  When I create contacts and view them within the CRM application I can see they are created by my account.  My account is set up as a system administrator.

I am now attempting to add a new item to a picklist attribute but I am getting a 401 error (unauthorized).  I am logged in as a system administrator so it shouldn't be a rights issue.  I had myself added to the PrivUserGroup for CRM but that didn't help either.

Below is the code I am using, pretty much right from the SDK example.

 Any ideas?
Thanks in advance.


MetadataService mdService = GetCrmMetadataService(ConfigurationSettings.AppSettings["hostName"], ConfigurationSettings.AppSettings["OrgName"]);

// Create new picklist label
crmMetadata.CrmLabel newCrmLabel = new crmMetadata.CrmLabel();
CrmNumber langCode = new crmMetadata.CrmNumber();
crmMetadata.LocLabel englishLabel = new crmMetadata.LocLabel();

// Use US English lang code
langCode.Value = 1033;
englishLabel.LanguageCode = langCode;
englishLabel.Label = picklistLabel;
newCrmLabel.LocLabels =
new crmMetadata.LocLabel[] { englishLabel };

// Create a request.
InsertOptionValueRequest insertRequest = new InsertOptionValueRequest();
insertRequest.AttributeLogicalName = picklistName;
insertRequest.EntityLogicalName = entityType;
insertRequest.Label = newCrmLabel;
insertRequest.Value =
new crmMetadata.CrmNumber();
insertRequest.Value.Value = 200000;

// Send the message.
InsertOptionValueResponse insertResponse = (InsertOptionValueResponse)mdSevice.Execute(insertRequest);



Chevva responded on 19 Sep 2008 6:56 AM

I'm getting the same (401); did you get this sorted?


CSusan responded on 9 Jan 2009 9:19 PM


I need to retreive the picklist items values using MetaData service.
I am not able to connect to the MetaData Service in my production environment which is an IFD deployment.
It gives me an "401 : UnAuthorised Error." . The  same code works well in my development environment which is a VPC environment.
Pls find code below for the same.

Also when I look at the log file the error oroiginates from the line
"RetrieveAttributeResponse amRes = (RetrieveAttributeResponse)service.Execute(attribReq);"

The metadata service url created is fine and it returns the meta methods when pasted on a web browser.

public static MetadataService GetMetaDataService()
   MetadataService service = new MetadataService();
   string organization = "PROD"; //ConfigurationManager.AppSettings["OrgName"].ToString();
   string server = "PRODUCTION";//ConfigurationManager.AppSettings["ServerName"].ToString();
   string domain = "HOSTING";//ConfigurationManager.AppSettings["CRMDomain"].ToString();
   string username = "Administrator";//ConfigurationManager.AppSettings["CRMUserName"].ToString();
   string password = "XXXXXX";//ConfigurationManager.AppSettings["CRMPassword"].ToString();
   string VDBAuthenticationType = "SPLA";//ConfigurationManager.AppSettings["AuthenticationType"].ToString();
   service.Credentials = new System.Net.NetworkCredential(username, password,domain);

   string Server = "PRODUCTION"; //ConfigurationManager.AppSettings["ServerName"].ToString();
   string Port = "80";//ConfigurationManager.AppSettings["PortNumber"].ToString();

   service.CrmAuthenticationTokenValue = new MetaSdk.CrmAuthenticationToken();
   service.CrmAuthenticationTokenValue.OrganizationName = organization;

   if (Port != "")
    service.Url = string.Format("http://{0}:{1}/mscrmservices/2007/metadataservice.asmx", Server, Port);
    service.Url = string.Format("http://{0}/mscrmservices/2007/metadataservice.asmx", Server);
   catch(SoapException ex)
    ErrorWriter(ex,"Meta Sevice Error");

Also the code for Retreiving picklist Values is here
public static int GetPickListItemValue(string entityName, string pickListName, string itemLabel)

        int itemValue = 0;

        MetadataService service = new MetadataService();

        service = GetMetaDataService();

        RetrieveAttributeRequest attribReq = new RetrieveAttributeRequest();
        attribReq.EntityLogicalName = entityName;
        attribReq.LogicalName = pickListName;

        // Get the attribute metadata for the pickListName attribute.
        RetrieveAttributeResponse amRes = (RetrieveAttributeResponse)service.Execute(attribReq);

        AttributeMetadata am = amRes.AttributeMetadata;

        PicklistAttributeMetadata listData = (PicklistAttributeMetadata)am;

        foreach (Option option in listData.Options)
            foreach (LocLabel label in option.Label.LocLabels)
                // Show US English value:label pairs
                if (label.LanguageCode.Value == 1033)

                    if (label.Label.Equals(itemLabel))
                        itemValue = option.Value.Value;
 catch(SoapException ex)
    ErrorWriter(ex,"Error in Retreiving PickList Values, Metadata Sevice Error");

        return itemValue;


Pls help me out.
I am stuck with this code as it works fine in mu[y VPC but gives "401 error" in the production.
Also I have verified that the logged in user has the righta for Entity, Relationship and Attribute as he is the administarator of CRM.

I have also added </clear> tag in the web.config file but no sucess.