1

I have created a Silverlight Application that runs in a SharePoint 2010 web part.

One requirement I have is to give the user a prompt to upload a file, and then send it into a specific Document Library with the additional Fields 'First' and 'Last' (Name corresponding to whom the document is from).

I am able to create the document and upload it to SharePoint. At this point however I hit an impasse. The normal means (as far as I'm aware) to add a Document to a Document Library is something similar to the following...

ClientContext context = new ClientContext(ApplicationContext.Current.Url);
context.Load(context.Web);
List docList = context.Web.Lists.GetByTitle("MyDocLib");
context.Load(docList);

docList.RootFolder.Files.Add(myFile); // Assuming of course the file is retrieved already
context.ExecuteQuery(); // Or ExecuteQueryAsync for more functionality

Unfortunately this leaves no room to add in the extra fields i need.

Furthermore the only thing received if i were to use something like

File file = docList.RootFolder.Files.Add(myFile);

does not seem to have an association with the library to build off of and then run another ExecuteQuery()

I would imagine there would be a way of treating the Document Library as a List (I'm fairly sure from a technical perspective they amount to the same thing) for its item creation, however I do not know how to create it properly.

If someone could give me advice on how to proceed, it would be much appreciated.

Thank you

1 Answer 1

5

You're almost there, using your Files.Add method as shown you can then set properties like this:

File file = docList.RootFolder.Files.Add(myFile);
ListItem item = file.ListItemAllFields;
item["Title"] = "Super Awesome File!";
item.Update();

References:

1
  • Great! Thanks, I'll try it right now and then set it as an answer!
    – Zork
    Mar 16, 2012 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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