MRu
MRu

Reputation: 1395

How to add value in Document Library Column

i'm trying to figure out how to fill value in Document Library column using web part. Now im using this code to add value in Task List column using this code. I'm looking solution on how to use something simillar to add value in Document Library Column. Examples and Suggestion will be much appreciated. Thanks and Regards.

SPWeb oWeb = SPContext.Current.Web;

SPListItemCollection listItems = oWeb.Lists["DDC"].Items;

SPListItem oItem = listItems.Add();

oItem["Title"] = "New Task";

oItem.Update();

Upvotes: 0

Views: 1561

Answers (1)

Amit Thakkar
Amit Thakkar

Reputation: 356

Please refer below code:

SPWeb oWeb = SPContext.Current.Web;
SPList lstTarget = oWeb.Lists.TryGetList("DDC");

Hashtable metaData = new Hashtable();

metaData.Add("Field1 Name", "Field1 Value");
metaData.Add("Field2 Name", "Field2 Value");
metaData.Add("Field3 Name", "Field3 Value");

byte[] bytes = File.ReadAllBytes("c:\folder\myfile.txt"); //Set path to file to be uploaded in document library.

oWeb.AllowUnsafeUpdates = true;
SPFile destfile = lstTarget.RootFolder.Files.Add("myfile.txt", bytes, metaData, true);
oWeb.AllowUnsafeUpdates = false;

Upvotes: 1

Related Questions