Question

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

Was it helpful?

Solution

Below code will create and update a document:

public void UpdateAndCreateFile(SPWeb web)
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    string TemplateUrl = string.Empty;
                    try
                    {
                        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
                        folders.Add(foldername);
                    }

                    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);
                        uploadedFile.CheckOut();
                        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.
                        listitem.Update();
                        uploadedFile.Update();
                        uploadedFile.CheckIn(string.Empty);
                        web.AllowUnsafeUpdates = false;
                    }
                });






            }
            catch (Exception ex)
            {
                // handle exception here
            }

        }

below couple of links which helps you :

Source

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top