1

I'm working with MFiles API...

I want to pass a propertyDef to a propertyValue...

This code is working... but I have to create the MFiles object first.

 ObjectVersionAndProperties objVersion = 
 mFilesStructure.MFileVault.ObjectOperations.CreateNewObject(objTypeID, 
 propValues);

 var testPropValues = new PropertyValues();
 testPropValues = FilesStructure.MFileVault.ObjectPropertyOperations.GetProperties(objVersion.ObjVer);

  var testPropValue = new PropertyValue();
  testPropValue = testPropValues.SearchForProperty(typeClientID);

it work fine "testPropValue" has all the property set correctly espacially the DataType... but don't want to create the MFiles at first...

This should do the same, in my opinion but doesn't

var test = new PropertyDef();
test = mFilesStructure.MFileVault.PropertyDefOperations.GetPropertyDef(typeClientID);
var testPropValue = new PropertyValue();
testPropValue.PropertyDef = test.ID;

the properties doesn't setup correctly...

Any one can help

Best regards,

Steph

Simo Kivistö
  • 4,247
  • 3
  • 38
  • 42
steph
  • 49
  • 3

1 Answers1

2

I just stumbled across this looking for something else and thought I might help.

You actually have it a little backwards. The creation of the new object is actually the last step in the process. You need to create a collection of PropertyValues() by creating each individual PropertyValue() and then adding them to the collection.

So something like this:

    public static PropertyValue GetPropertyValue(int propertyDefId, object value)
    {
        //resolve property def by ID
        PropertyDef propertyDef = Vault.PropertyDefOperations.GetPropertyDef(propertyDefId);

        //create the property value with prop def ID and value
        return GetPropertyValue(propertyDefId, propertyDef.DataType, value);
    }

    public static PropertyValue GetPropertyValue(int propertyDefId, MFDataType dataType, object value)
    {
        PropertyValue propertyValue = new PropertyValue();
        propertyValue.PropertyDef = propertyDefId;
        propertyValue.TypedValue.SetValue(dataType, value);

        return propertyValue;
    }

    public static ObjectVersionAndProperties CreateDocument(PropertyValues propertyValues, string filepath, Vault vault)
    {
        // Create the Source File object from the filepath.
        SourceObjectFile sourceFile = new SourceObjectFile();
        sourceFile.SourceFilePath = filepath;
        sourceFile.Extension = Path.GetExtension(filepath).TrimStart('.');
        sourceFile.Title = Path.GetFileNameWithoutExtension(filepath).TrimEnd('.');

        // Create the document object.
        return vault.ObjectOperations.CreateNewSFDObject((int)MFBuiltInObjectType.MFBuiltInObjectTypeDocument,
            propertyValues, sourceFile, true);
    }

Once you set up the above functions you could call them like:

     //If the document doesn't exist, go ahead and create a new one
     //creat and add all the properties
     PropertyValues props = new PropertyValues();
     //class
     props.Add(-1, HelperMF.GetClassPropertyValue(classId, env.Vault));
     //job
     int jobId = env.Vault.ValueListItemOperations.GetValueListItemByDisplayID(Structure.ObjType.Job.ID, jobDisplayId).ID;
     props.Add(-1, HelperMF.GetPropertyValue(Properties.Job.ID, jobId, env.Vault));
     //estimates
     props.Add(-1, HelperMF.GetPropertyValueFromListOfDisplayIds(env.Vault, Properties.Estimate.ID,
                      MFDataType.MFDatatypeMultiSelectLookup, Structure.ObjType.Estimate.ID, estimateDisplayIds));
     //Add the relationship to the return doc that was uploaded
     props.Add(-1, HelperMF.GetPropertyValue(Properties.Document.ID, movingDocId, env.Vault));

     //create the new object in the vault
     ObjectVersionAndProperties newDoc = HelperMF.CreateDocument(props, docDownloadPath, env.Vault);

I use a lot of help functions and classes but you should get the gist from my samples. Also, I would highly recommend you use the M-Files community website for research as they have a lot of code samples there geared specifically for M-Files.

https://community.m-files.com/

Also, if you don't already, use the API documentation as it also includes code samples.

http://www.m-files.com/api/documentation/2015.2/

Hopefully this helps, Mike

Mike
  • 21
  • 3