1

I have a document library with a custom column. I want to add a file programatically, as well as a value for the custom column. I am posting the file via an ASP.NET application page. Here is what I have:

HttpPostedFile file = Request.Files["MyFile"];
web.AllowUnsafeUpdates = true;
SPFolder library = web.Folders["MyLibrary"];
SPFile spFile = library.Files.Add(file.FileName, file.InputStream);
/* The next two lines are an attempt to add the custom column value */
spFile.Item["CustomColumn"] = "CustomColumnValue";
spFile.Update();
library.Update();
web.AllowUnsafeUpdates = false;

It does add the file to the document library, but it does not add the value for the custom column. What am I doing wrong?

1

This works for me.

spfile.Item["Title"] = "Workk";
spfile.Item.Update();
spfile.Update();
  • ... for me too! – helb Apr 30 '18 at 13:52
0

I found out what I was going wrong.

Instead of:

spFile.Item["CustomColumn"] = "CustomColumnValue";
spFile.Update();

do this:

spFile.Item.Properties["CustomColumn"] = "CustomColumnValue";
spFile.Item.SystemUpdate();
spFile.Update();
0

Your solution is right, this is another solution using a hashtable variable: you can upload the file from a SPFolder or SPWeb as following:

// Add custom value to the column library
System.Collections.Hashtable properties = new System.Collections.Hashtable();
properties.Add("My custom column name", "My value");

web.Files.Add("CustomFolder/" + name + ".txt", Encoding.UTF8.GetBytes(sb.ToString()), properties);

And the file will be uploaded with the value you set on the custom column.

I know is kind of late, but maybe it would be useful for someone else.

Regards

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.