Exemplo n.º 1
0
        private void UpdateOrGenerateSnippets()
        {
            rchTxtBxWrapperFunction.Text = SnippetsGenerator.GenerateWrapperFunction();
            var selectedOperation   = cbBxOperationType.SelectedItem as OperationTypeInfo;
            var selectedAssociation = cbBxAssociateWith.SelectedItem as AssociationInfo;

            if (selectedOperation != null)
            {
                APIOperationTypes operationType = selectedOperation.Type;
                bool addFields = chBxUseSelectedFields.Checked;
                List <WebAPIAttributeItemModel> selectedAttributes = chkdLstBxAllAttibutes.CheckedItems.Cast <WebAPIAttributeItemModel>().ToList();
                string snippet = SnippetsGenerator.GenerateSnippet(Model.SelectedEntityInfo, selectedAttributes, selectedOperation.Type, selectedAssociation, addFields);
                rchTxtBoxOperation.Text = snippet;
            }
        }
 private OperationTypeInfo(string helpMessage, APIOperationTypes type)
 {
     Message = helpMessage;
     Type    = type;
 }
Exemplo n.º 3
0
        private static string GenerateJsonFromFields(List <WebAPIAttributeItemModel> selectedAttribute, APIOperationTypes operationType)
        {
            StringBuilder jsonBuilder = new StringBuilder();

            foreach (var attribute in selectedAttribute)
            {
                if (operationType == APIOperationTypes.BasicCreate && !attribute.IsValidForCreate)
                {
                    continue;
                }
                if ((operationType == APIOperationTypes.UpdateSingle || operationType == APIOperationTypes.BasicUpdate) && !attribute.IsValidForUpdate)
                {
                    continue;
                }

                switch (attribute.DataType)
                {
                case AttributeTypeCode.Boolean:
                    jsonBuilder.Append("\t\t\"" + attribute.LogicalName + "\":false");
                    break;

                case AttributeTypeCode.Integer:
                case AttributeTypeCode.BigInt:
                case AttributeTypeCode.Picklist:
                case AttributeTypeCode.State:
                case AttributeTypeCode.Status:
                    jsonBuilder.Append("\t\t\"" + attribute.LogicalName + "\":0");
                    break;

                case AttributeTypeCode.Double:
                case AttributeTypeCode.Decimal:
                case AttributeTypeCode.Money:
                    jsonBuilder.Append("\t\t\"" + attribute.LogicalName + "\":0.0");
                    break;

                case AttributeTypeCode.Memo:
                case AttributeTypeCode.String:

                    jsonBuilder.Append("\t\t\"" + attribute.LogicalName + "\":\"Some String Value\"");
                    break;

                case AttributeTypeCode.DateTime:
                    jsonBuilder.Append("\t\t\"" + attribute.LogicalName + "\":\"Some Date Value\"");

                    break;

                case AttributeTypeCode.Uniqueidentifier:
                    jsonBuilder.Append("\t\t\"" + attribute.LogicalName + "\":\"" + Guid.Empty.ToString() + "\"");
                    break;

                case AttributeTypeCode.Lookup:
                case AttributeTypeCode.Customer:

                    if (operationType == APIOperationTypes.BasicUpdate || operationType == APIOperationTypes.UpdateSingle)
                    {
                        var relatedEntityColelcitonName = attribute.RelatedEntityCollectionName;
                        jsonBuilder.Append("\t\t\"" + attribute.WebAPIName + "@odata.bind\":portalurl+\"/_api/" + relatedEntityColelcitonName + "(11111111-1111-1111-1111-111111111111)" + "\"");
                    }
                    else
                    {
                        jsonBuilder.AppendLine("\t\t\"" + attribute.WebAPIName + "\":");
                        jsonBuilder.AppendLine("\t\t{");
                        jsonBuilder.AppendLine("\t\t\t /*Add related entity fields. Make sure to add the required fields for the related entity. Also, make sure that the related entity is enabled for Web API and has proper entity permissions. To learn more about deep insert, take a look at https://docs.microsoft.com/en-us/powerapps/developer/data-platform/webapi/create-entity-web-api#create-related-entity-records-in-one-operation */");
                        jsonBuilder.Append("\t\t}");
                    }


                    break;

                case AttributeTypeCode.Virtual:
                    jsonBuilder.Append("\t\t\"" + attribute.LogicalName + "\":\"This is a virtual field. An example of this field is entity image on the contact entity and it needs to be a base64 string. For other virtual fields values, please check Microsoft documentation.\"");
                    break;

                default:
                    jsonBuilder.Append("\t\t\"" + attribute.LogicalName + "\":\"Unknown field type. please check Microsoft documentation for the proper format of the this field value.\"");
                    break;
                }

                if (selectedAttribute.IndexOf(attribute) < selectedAttribute.Count - 1)
                {
                    jsonBuilder.AppendLine(",");
                }
            }
            jsonBuilder.AppendLine("");
            return(jsonBuilder.ToString());
        }
Exemplo n.º 4
0
        private static string GenerateBasicCreateSnippet(EntityItemModel selectedEntityInfo, List <WebAPIAttributeItemModel> selectedAttributes, APIOperationTypes opType, AssociationInfo association, bool addFields)
        {
            string        json               = addFields ? GenerateJsonFromFields(selectedAttributes, opType) : "";
            StringBuilder jsonObject         = new StringBuilder();
            StringBuilder snippetTextBuilder = new StringBuilder();

            snippetTextBuilder.AppendLine("/*This is a sample create snippet using the POST operator. Use this snippet inside your record creation logic. Replace or modify the data object properties per your needs. Make sure that these properies are enabled for web api.*/");


            jsonObject.AppendLine("var dataObject={");
            jsonObject.AppendLine(json);
            jsonObject.AppendLine("};");


            snippetTextBuilder.AppendLine(jsonObject.ToString());


            snippetTextBuilder.AppendLine("webapi.safeAjax({");
            snippetTextBuilder.AppendLine("\ttype: \"POST\",");
            snippetTextBuilder.AppendLine("\turl: \"/_api/" + selectedEntityInfo.CollectionName + "\",");
            snippetTextBuilder.AppendLine("\tcontentType:\"application/json\",");
            snippetTextBuilder.AppendLine("\tdata: JSON.stringify(dataObject),");
            snippetTextBuilder.AppendLine("\tsuccess: function(res, status, xhr) {");
            snippetTextBuilder.AppendLine("\t\t//print id of newly created entity record");
            snippetTextBuilder.AppendLine("\t\tconsole.log(\"entityID: \" + xhr.getResponseHeader(\"entityid\"))");
            snippetTextBuilder.AppendLine("\t}");
            snippetTextBuilder.AppendLine("});");


            return(snippetTextBuilder.ToString());
        }
Exemplo n.º 5
0
        private static string GenerateBasicUpdateSnippet(EntityItemModel selectedEntityInfo, List <WebAPIAttributeItemModel> selectedAttributes, APIOperationTypes opType, AssociationInfo association, bool addFields)
        {
            StringBuilder snippetTextBuilder = new StringBuilder();

            snippetTextBuilder.AppendLine("/*This is a sample basic update snippet using the PATCH operator. You need to specify the ID of the record being updated and the JSON data object for the fields you want to update.*/");

            string        json       = addFields ? GenerateJsonFromFields(selectedAttributes, APIOperationTypes.BasicUpdate) : "";
            StringBuilder jsonObject = new StringBuilder();

            jsonObject.AppendLine("var dataObject={");
            jsonObject.AppendLine(json);
            jsonObject.AppendLine("};");


            snippetTextBuilder.AppendLine(jsonObject.ToString());


            snippetTextBuilder.AppendLine("webapi.safeAjax({");
            snippetTextBuilder.AppendLine("\ttype: \"PATCH\",");
            snippetTextBuilder.AppendLine("\turl: \"/_api/" + selectedEntityInfo.CollectionName + "(00000000-0000-0000-0000-000000000000)\",");
            snippetTextBuilder.AppendLine("\tcontentType:\"application/json\",");
            snippetTextBuilder.AppendLine("\tdata: JSON.stringify(dataObject),");
            snippetTextBuilder.AppendLine("\tsuccess: function(res) {");
            snippetTextBuilder.AppendLine("\t\tconsole.log(res)");
            snippetTextBuilder.AppendLine("\t}");
            snippetTextBuilder.AppendLine("});");


            return(snippetTextBuilder.ToString());
        }
Exemplo n.º 6
0
        public static string GenerateSnippet(EntityItemModel selectedEntityInfo, List <WebAPIAttributeItemModel> selectedAttributes, APIOperationTypes opType, AssociationInfo association, bool addFields)
        {
            switch (opType)
            {
            case APIOperationTypes.BasicCreate:
                return(GenerateBasicCreateSnippet(selectedEntityInfo, selectedAttributes, opType, association, addFields));

            case APIOperationTypes.UpdateSingle:
                return(GenerateUpdateSingleSnippet(selectedEntityInfo, selectedAttributes, addFields));

            case APIOperationTypes.BasicUpdate:
                return(GenerateBasicUpdateSnippet(selectedEntityInfo, selectedAttributes, opType, association, addFields));

            case APIOperationTypes.BasicDelete:
                return(GenerateBasicDeleteSnippet(selectedEntityInfo, selectedAttributes));

            case APIOperationTypes.DeleteSingle:
                return(GenerateDeleteSingleSnippet(selectedEntityInfo, selectedAttributes, addFields));

            case APIOperationTypes.AssociateDisassociate:

                break;

            default:
                throw new NotImplementedException("This operation is not implemented yet");
            }

            return("");
        }