locked
Create Entity Members using Web Service methods RRS feed

  • Question

  • We are getting error as MemberCode is required when trying to add Members to Entity using CreateEntityMember function. Please refer below code snippet, are we missing anything here. We already have set Code as attribute but still it is asking for setting MemberCode. If there is any examples wherein we can refer to implement the same...

     

    EntityMembers em = new EntityMembers();

    em.EntityId =

    new Identifier() { Name = MyEntity.Identifier.Name };

    em.ModelId =

    new Identifier() { Name = MyModel.Identifier.Name };

    em.VersionId =

    new Identifier() { Name = "VERSION_1" };

     

    int memberNo = 0;

    em.Members =

    new Member[]{new Member()};

     

    em.Members[0].Attributes =

    new CreateSchema.WEBSERVICE.Attribute[100];

     

     

    do

    {

     

    if (xr.NodeType != XmlNodeType.Whitespace)

    {

    WEBSERVICE.

    Attribute attr = new WEBSERVICE.Attribute();

     

    if (xr["membertype"] == "number")

    {

    attr.Type =

    AttributeValueType.Number;

    }

     

    else if (xr["membertype"] == "string")

    {

    attr.Type =

    AttributeValueType.String;

    }

     

    else if (xr["membertype"] == "string")

    {

    attr.Type =

    AttributeValueType.String;

    }

     

    else if (xr["membertype"] == "datetime")

    {

    attr.Type =

    AttributeValueType.DateTime;

    }

     

    else if (xr["membertype"] == "domain")

    {

    attr.Type =

    AttributeValueType.Domain;

    }

     

    else if (xr["membertype"] == "file")

    {

    attr.Type =

    AttributeValueType.File;

    }

     

    else

    {

    attr.Type =

    AttributeValueType.NotSpecified;

    }

     

    attr.Value = xr[

    "value"];

     

    MetadataResultOptions mro = new MetadataResultOptions();

    mro.Attributes =

    ResultType.Identifiers;

     

    MetadataSearchCriteria msc = new MetadataSearchCriteria();

    msc.Attributes =

    new Identifier[] { new Identifier() { Name = xr["name"] } };

     

    OperationResult op = new OperationResult();

     

    Metadata md = mdsProxy.MetadataGet(new International(), mro, msc, out op);

    attr.Identifier =

    new Identifier() { Name = xr["name"], Id = md.Attributes[0].Identifier.Id };

    em.Members[0].Attributes[memberNo] = attr;

    memberNo++;

     

    }

    xr.Read();

    }

    while (xr.NodeType == XmlNodeType.Whitespace || xr.NodeType == XmlNodeType.Element || xr.Name.Trim().ToLower() == "attributemember");

     

    OperationResult opr = mdsProxy.EntityMembersCreate(new International(), em);

    Friday, October 29, 2010 1:29 PM

Answers

  • Hello Vipul,

     

    Here is an example of entity member create (you can check my mds manager source code for full  examples: http://social.msdn.microsoft.com/Forums/en/sqlmds/thread/837b787a-f55a-4e6a-8e58-6442c6841fc2)

     

     public void ManageMember(MemberAction action)
        {
          try
          {
            Cursor.Current = Cursors.WaitCursor;
            if (!string.IsNullOrEmpty(txtAttributeValue.Text.Trim()) && ucManageEntities1.lstEntities.SelectedItem != null && lstModels.SelectedItem != null && lstVersions.SelectedItem != null)
            {
              EntityMembers em = new EntityMembers();
              em.EntityId = ((CustomEntity)ucManageEntities1.lstEntities.SelectedItem).entityId;
              em.ModelId = (Identifier)lstModels.SelectedItem;
              em.VersionId = ((CustomVersion)lstVersions.SelectedItem).Identifier;
              em.MemberType = new MemberType();
              em.MemberType = MemberType.Leaf;
              em.Members = new Collection<Member>();
              Member m = new Member();
              if (action == MemberAction.UpdateMember)
              {
                m.MemberId = ((CustomMember)ucManageMembers1.lstMembers.SelectedItem).MemberId;
                // m.SecurityPermission = SecurityPermission.Update;
              }
              else
              {
                m.MemberId = new MemberIdentifier();
              }
              m.MemberId.Name = txtAttributeName.Text.Trim();
              m.MemberId.Code = txtAttributeValue.Text.Trim();
              if (action == MemberAction.UpdateMember)
              {
                m.Attributes = new Collection<Common.ServiceReference1.Attribute>();
                Common.ServiceReference1.Attribute attr1 = new Common.ServiceReference1.Attribute();
                attr1.Identifier = new Identifier();
                attr1.Identifier.Name = "Code";
                attr1.Value = new char();
                attr1.Value = m.MemberId.Code;
                m.Attributes.Add(attr1);
    
                Common.ServiceReference1.Attribute attr2 = new Common.ServiceReference1.Attribute();
                attr2.Identifier = new Identifier();
                attr2.Identifier.Name = "Name";
                attr2.Value = new char();
                attr2.Value = m.MemberId.Name;
                m.Attributes.Add(attr2);
              }
    
              em.Members.Add(m);
              OperationResult or = new OperationResult();
    
              using (ServiceClient c = MDS_WSConnect.CreateMdsProxy())
              {
                if (action == MemberAction.AddMember)
                  or = c.EntityMembersCreate(new International(), em);
    
                if (action == MemberAction.UpdateMember)
                {
                  or = c.EntityMembersUpdate(new International(), em);
                }
                if (action == MemberAction.DeleteMember)
                  or = c.EntityMembersDelete(new International(), em);
              }
              StringBuilder sb = new StringBuilder();
              if (or.Errors.Count > 0)
              {
                foreach (Error err in or.Errors)
                {
                  sb.AppendLine(em.Members[0].MemberId.Code + " - " + err.Code + " - " + err.Description);
                }
                MessageBox.Show(sb.ToString(), "Errors");
    
    
              }
              RefreshLstMembersAttributes((Identifier)lstModels.SelectedItem, (CustomEntity)ucManageEntities1.lstEntities.SelectedItem, ((CustomVersion)lstVersions.SelectedItem).Identifier);
            }
            else
              lblError.Text = "Member Code cannot be empty!";
          }
          catch (Exception ex)
          {
            lblError.Text = ex.Message;
            throw ex;
          }
          finally
          {
            Cursor.Current = Cursors.Default;
          }
    
        }
    
    
    


    Note: Please vote/mark the post as answered if it answers your question/helps to solve your problem. _________________________________________________________ Regards, Xavier Averbouch, Consultant, Avanade France
    Friday, October 29, 2010 8:18 PM