web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Metadata Execute 401 Unauthorized error

(0) ShareShare
ReportReport
Posted on by 40

Hi,

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 asp.net 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();
crmMetadata.
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);

 

 

*This post is locked for comments

I have the same question (0)
  • Chevva Profile Picture
    95 on at

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

    Cheers.

  • Community Member Profile Picture
    on at

    Hi,

    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();
       try{
       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);
       else
        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;
     try
     {

            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.


    Regards
    Susan

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans