4

I have a custom button on my application page to open a new document from a document template, like you would do directly on a list. The code is similar to:

<a href="http://Site/test/documents" onClick="createNewDocumentWithProgID  
('http:\u002f\u002fsite\u002ftest\u002fdocuments\u002fForms\u002ftemplate.doc',    
'http:\u002f\u002fsite\u002ftest\u002fdocuments',    
'SharePoint.OpenDocuments', false)">Click to create document</a>

This opens my document template in Word without any problem. The document template is coupled to a content type, so all metadata fields are defined.

But now I do want to set metadata (properties) in the newly created document, preferably before the document gets opened so the data is visibile in the Document Information Panel inside Word. This data has to be set dynamically because it's based on parameters defined in the application page. Is this possible?

3 Answers 3

1

Document Information Panel (this is how "Document Properties window inside Word" actually called) is rendered according to a document library content type. So, when your parameters on the application page you mentioned are saved, you should probably apply default values to your document library, using SPField.DefaultValue.

This can be done easily:

var documentLibrary = web.GetListFromUrl("http://Site/test/documents/Forms/AllItems.aspx");
var field = documentLibrary.Fields["YourFieldName"];
field.DefaultValue = "Your default value";
field.Update();
3
  • I'd like to stay away from default values, since this will result in incorrect data (as there is no real default option except for an empty string). The parameters on my page come from different objects (not linked to the document library) and the values should be entered as plain text in the Document Information Panel (couldn't get on the name, so tnx) so they metadata gets saved correctly. The enduser isn't expected to know all values by heart (also some do and want to check for correctness).
    – Bart
    Commented May 17, 2011 at 14:07
  • What do you mean? When you push MS Word open with createNewDocumentWithProgID, document does not exist in Document Library yet. At that moment, MS Word fetches your document template and plus, it fetches it's content type information. Based on the document template, document itself is created. Based on the content type, Word creates DIP. The only way to pass concrete values to DIP here is to use default values of the content type, I think. And if you will change default values each time before user presses your button, you will get your DIP set to concrete values. Am I missing something? Commented May 17, 2011 at 17:16
  • 1
    Changing default values of the content type with the parameters for the current user when the page loads won't work if you have multiple users in a high concurrency site.
    – Bart
    Commented May 23, 2011 at 6:42
1

So perhaps this is being a little cheeky, but perhaps try the following:

  1. Create a library with content type X
  2. Create a new document of type X. Fill out the properties.
  3. Download the document to your desktop and delete it from the library
  4. Convert the document to a document template and upload into the content type X as the default template

I have no idea if that will work, but it should only take 10-15 minutes to test.

2
  • This won't help for the few properties that have to be different for every document created and should be loaded with data from the application page.
    – Bart
    Commented May 17, 2011 at 15:43
  • So far as I know, the only way you could force this, you'd have to markup the content with tags and use code to push the specific data into the document. Commented May 17, 2011 at 16:30
1

With the remarks of Neil and omlin I opted to do a small tweak on the initial idea: When the user clicks the 'new item' button in the custom application page, I create a new document from the document template and place it in the correct subfolder (which I know according to the parameters available). Once this is done, I register a piece of javascript that triggers the 'open document' javascript code. For the end-user it seems like he's opening a new file, with the only difference that we just created it in the correct place and filled the metadata.

SPContentType contentType = list.ContentTypes["ctypename"];

// get the document template
string templateUrl = l.ContentTypes["ctypename"].DocumentTemplateUrl;
SPFile sourceFile = web.GetFile(templateUrl);

if (sourceFile != null)
{
    string fileName = CreateNewFileName();
    SPFile file = folder.Files.Add(fileName, sourceFile.OpenBinary(), true); // overwrite is true
    file.Item["ContentType"] = "ctypename";

            // set other fields
            ...

    file.Item.Update();
    file.Update();
    folder.Update();
}

// open the document
String scriptString = "editDocumentWithProgID2(\"" + SPContext.Current.Site.Url +
       "/Listname/SubFoldername/Filename.doc\", \"\", \"SharePoint.OpenDocuments\", \"0\", \"" +
       SPContext.Current.Site.Url +
       "\", \"0\")";

// popup window to open document
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "click", scriptString, true);

The button itself is placed in an AJAX updatepanel. This enables me execute the code without a full page postback.

Your Answer

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

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