1

My requirement is to create a word document based on the information in SharePoint lists on the site.

For dealing with the word I have three options: "Docx"library. (CodePlex Project) "OpenXML" "Office Interop"

Regardless of which approach I choose, I want to know how can I create a new word document item in (for example) "Shared Documents" without first saving the file to the disk ?

If it is not possible, where in the server can I save my word document and then upload it into SharePoint ? wherever I try to save it says "Access to path is denied".

Do you have any Idea ?

2

Below code created a CreateDocument method which will create a new document and and will add some text into it. It then calls AddToSharePoint method which will add the newly created document to the “Shared Documents” library of SharePoint.

public void CreateDocument(string docName)
{

using (MemoryStream memStream= new MemoryStream())
{

// Create a Wordprocessing document.
using (WordprocessingDocument doc = WordprocessingDocument.Create(memStream, WordprocessingDocumentType.Document))
{

// Add a new main document part.
doc.AddMainDocumentPart();

// Create the Document DOM.
doc.MainDocumentPart.Document =
new Document(
new Body(
new Paragraph(
new Run(
new Text(“Its a new Document!”)))));

// Save changes to the main document part.
doc.MainDocumentPart.Document.Save();

//Adding Document to SharePoint
AddToSharePoint(memStream,docName)
}
}
}

protected void AddToSharePoint(MemoryStream memStream,string fileName)
{

using (SPSite spSite = new SPSite(siteUrl))
{

//Get the document library object
SPList docLib = spSite.RootWeb.Lists["Shared Documents"];

SPFile file = docLib.RootFolder.Files.Add(fileName, memoryStream, true);

file.Update();

}

Source: Programmatically Create and upload document in Sharepoint 2010 document library

To re-open the document form SharePoint document library see the post here

For Access Denied error, did you try to use the UNC path?

  • No, I don't know about UNC path. I just put the filename itself or tried to write the full path like "c:\users\dev1\desktop" – Pouyan Nov 3 '14 at 20:02
  • UNC mean when you access a folder \\servername\sharefodler when you share a folder on server and Give your App Pool and Farm Admin account full right. – Waqas Sarwar MVP Nov 3 '14 at 20:04
3

You can use OpenXML sdk for creating word documents.

http://msdn.microsoft.com/en-us/library/dd440953(v=office.12).aspx

Instead of saving the document, you should create it using a MemoryStream and use that Stream while uploading the document to the library.

Here is a similar thread that discuss this approach

1

No need for custom code required - use SharePoint Designer 2013 ONLY.

What you can do is, using SharePoint Designer 2013, create a Workflow that uses the data from a List to create an item in a Library. You create a document library with the same fields as the list, and the workflow creates the item in the document library and passes over the values of the fields. The document template for the library will be customized, and you can easily insert the SharePoint list fields into the document using Quick Parts. Finally, set up a Custom Action that runs the workflow on a selected item.

The basic process is : Create Site Columns; Create Content Types; Create List and Library; Set up Document Template; Create Workflow.

There are a few "gotchas" though, so I will walk you through it:

Steps:

  1. Create the columns for the data as site columns
  2. Create a list item content type and a document content type and add the columns to each (its easiest to go to the Administration Web Page for the content type and click "Add Site Columns")
  3. Create a list and a library for each of the content types - enable custom content types and associate each with its respective content type.
  4. Set up the Document Library

    a. Make your custom content type the default, and remove the old "document" content type.

    b. Use the "Edit Document Template" button (on the ribbon) for the Library, open it, and then save it back to the site as a .docx file (not a .dotx file). You MUST save the template as a .docx file.

    c. Set the Document Template URL for the Library to the .docx file

You can now open you document template and add in the fields for the columns by going Insert -> Quick Part -> Document Property

  1. Create your workflow. In SharePoint Designer Hit the New button on the Workflows section of your LIST (not the library)

    a. Create a new SHAREPOINT 2010 Workflow (NOT 2013). It MUST be a 2010 Workflow.

    b. In the workflow, set up an Action to Create New List Item in your Document Library. You will then be prompted to pass over fields and values.

    c. You MUST pass over the "Content Type ID" field and set the value to your custom content type

    d. You MUST set the "Path and Name (*)" variable to something like [%Current Item:Title%].docx

    e. Now you can go to the Workflow Settings and set the Workflow to run automatically whenever you create a new item on the list, or go to the list and set up a Custom Action to run the workflow.

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.