0

I have 200 files I need to upload regularly to sharepoint 365, users sync that folder with onedrive. Now, some guy has come along and added a bunch of "properties" for my files, such as "Category" and "Target Audience" to sharepoint. Now, I am a lazy bugger, so I have created a ps1 file with a bunch of functions to create, delete folders and create, delete, upload files and have an automated workflow to upload the files to sharepoint automagically. I now need to programmatically set these "properties" on the files.

When I use the browser to look at my files, I can set values for these properties via a drop-down or radio buttons.

I found these two sources:

https://www.sptrenches.com/2014/12/write-and-get-user-profile-properties.html

https://social.technet.microsoft.com/wiki/contents/articles/21801.sharepoint-a-complete-guide-to-get...

These make me believe that this "should work":

//$file is Microsoft.SharePoint.Client.File


PS > $file.Properties.FieldValues["Category"]="Training"

PS > $file.Update()

However, when I do that, nothing happens. If I do:

//ctx is Microsoft.SharePoint.Client.ClientContext

PS > $file.Properties.FieldValues["Category"]="Training"
PS > $ctx.Load($file.Properties)

PS > $ctx.ExecuteQuery()

I get an error:

Exception calling "ExecuteQuery" with "0" argument(s): "Field or property "Category" does not exist."
At line:1 char:1
+ $ctx.ExecuteQuery()
+ ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ServerException

Now, I know there is a combination of the above that works, because I managed it twice, by mixing the above, but I am unable to get a consistent behavior.

Another thing, I tried to find the properties in the list in question:

//$List is Microsoft.SharePoint.Client.List



$List.Fields | select InternalName

I do not get Category or target audience

Now, i can manually set the fields on the files, but that is not an option for me ... any thoughts, what am I doing wrong ? If there is a way I can help you help me, please do advise ;-).

  • Which kind of field are those two fields, choice, lookup or managed metadata? – Kasper Bo Larsen Oct 20 '18 at 15:27
  • How could I find that out programmatically ? – thecarpy Oct 21 '18 at 13:29
  • It is easy to verify in the gui: go to library settings, locate the field and you should be able to see it. – Kasper Bo Larsen Oct 21 '18 at 15:39
0

Ok, I am an idiot.

$file.Poperties | get-member
[...]
Item                         ParameterizedProperty System.Object Item(string fieldName) {get;set;} <--- get AND set
[...]
FieldValues                  Property              System.Collections.Generic.Dictionary[string,System.Object] FieldValues {get;} <---- get only!

The following does work:

PS > $file.Properties.Item["Category"]="Training"
PS > $file.Update()
PS > $ctx.Load($file.Properties)
PS > $ctx.ExecuteQuery()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.