We are trying to accomplish this... From a page in a site, the user selects a value that is offered in a drop down based on who the user is in the organization.

This drop down is in place already and shows the values from which the user can choose.

When the user makes that choice, we update 5 other computed fields.

Furthermore, we found out how to programmatically create a document library with a content type defined with fields for the choice the user made plus the 5 computed fields.

This is the only part I can't seem to find:

How can I create a Word doc from the library (based on the library template) with those 6 fields already populated? BTW, this document creation should happen on the click of a button.

I would like to do this with CodeBehind.

Thanks! E

  • Is the fields added to the doc itself? If I am assuming correctly, You want to download the item you just created on the click of the button! – Taran Goel Jul 10 '15 at 13:30
  • Taran, that is correct. Once added to the library, I was going to issue a redirect to the url of the doc. The fields should be in the doc on download. – Eric Jul 10 '15 at 13:43
  • are you using client side object model or server side model to create the doc? And does the document get created on the click of button? – Taran Goel Jul 10 '15 at 13:46
  • Yes, the doc should get created at the click of a button (which is already implemented). Right now the button redirects to some other place. I think I would prefer to do this server side. – Eric Jul 10 '15 at 13:55
  • When you create a doc, you already have ID of the doc. Why don't you construct a URL with the help of it and redirect the user? Is there a problem with that? – Taran Goel Jul 10 '15 at 14:02

Below code will create and update a document:

public void UpdateAndCreateFile(SPWeb web)
                    string TemplateUrl = string.Empty;
                        TemplateUrl = "Your Document templated full path";
                    catch { }

                    web.AllowUnsafeUpdates = true;
                    SPList olist = web.Lists["Document Library"];
                    String url = olist.RootFolder.ServerRelativeUrl.ToString();

                    string foldername = Convert.ToString("Foldername in document library");
                    SPFolder newfolder = web.GetFolder(url + "/" + foldername);

                    if (!newfolder.Exists)
                        SPFolderCollection folders = web.GetFolder(url).SubFolders;
                        //Create new folder

                    SPFile file = null;

                    file = web.GetFile("Your Document templated full path");

                    if (file != null)
                        web.AllowUnsafeUpdates = true;
                        Stream readStream = file.OpenBinaryStream(); //file is SPFile type
                        SPFile uploadedFile = newfolder.Files.Add(newfolder.Url + @"/" + "NewDocName.docx", readStream, true);
                        SPListItem listitem = uploadedFile.Item;
                        // Details is mapped in document
                        listitem["Details"] = "this content will added in document";
//You can refer more columns here to update. Or even replace the content by reading the file.
                        web.AllowUnsafeUpdates = false;

            catch (Exception ex)
                // handle exception here


below couple of links which helps you :



If I understand you correctly there are 5 metedata,columns that automatically created when the word document is created. In your word template on the insert tab, go to the "Quick Parts" and select document properties. If those fields are attached to your content type you should see them there. Add them to your Word Document template wherever you want then to be displayed.. Hope this helps.

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.