Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm trying to create a new word document from a template using the following javascript code :

var word = new ActiveXObject("Word.Application");
var doc = word.Documents.Add("/server/docLib/ContentType/template.dotx");

before i display the word application, i want to initialize some content types properties :

doc.ContentTypeProperties.GetItemByInternalName("field1_internalName").Value = "MyValue"

The previous code works fine. But when I try to set a property of type Lookup, an exception is raised with no error description.

To see what was wrong, i used the following code to open an existing document with all properties filled.

var word = new ActiveXObject("Word.Application");
var doc = word.Documents.Open("/server/docLib/doc1.docx");
var ct = doc.ContentTypeProperties;
var lookupprop = ct.GetItemByInternalName("field1_internalName");

After debugging, I found that lookupprop is an object of type MetaProperty and contains somme fields like Type, ID... etc. and Value of type Variant. when i tried to acces the value, the same exception was raised.

My question is : Is it possible to set a lookup properties like this? and if yes how?

share|improve this question
add comment

1 Answer

Lookup columns are special and require special interaction, please read this excerpt from MSDN:

    function addNewSale(employeeId, salesachieved, customer, description) {
       // Get the current context
       var context = new SP.ClientContext.get_current();
       // Get the web
       var web = context.get_web();
       // Get the Sales list
       var list = web.get_lists().getByTitle('Sales');
       // create the ListItemInformational object
       var listItemInfo = new SP.ListItemCreationInformation();
       // add the item to the list
       var listItem = list.addItem(listItemInfo);
       // Assign Values for fields
       listItem.set_item('SalesAchieved', salesachieved);
       listItem.set_item('Customer', customer);
       listItem.set_item('Title', description);
       //Lookup field need to be catered using FieldLookUp value
       var employeeNameValue = new SP.FieldLookupValue();
       employeeNameValue.set_lookupId(employeeId);
       listItem.set_item('EmployeeName', employeeNameValue);
       // Adding value for a Date Field
       var currDate = new Date();
       listItem.set_item('SaleDate', currDate);
       listItem.update();
       //Make a query call to execute the above statements
       context.executeQueryAsync(Function.createDelegate(this, this.on_addSales_Success), Function.createDelegate(this, this.on_addSales_Failure));
    }
    function on_addSales_Success() {
       alert("Sale successfully created.");
    }
    function on_addSales_Failure() {
       alert("Error while creating new Sales.");
    }

Source: http://msdn.microsoft.com/en-us/library/hh372944%28v=office.14%29.aspx#odc_spe14_ta_WorkwithJSOM_ManipulatingData

share|improve this answer
    
Hi Hugh, thank you for the answer. My problem here is not to update an item using Javascript, but to launch a new document command and be able to initialize some fields before opening it using Word client. As mentioned in the first post, I did that successfully for some basic fields (Text, Choice etc), but with the lookups field an exception occurs when I try access the value. –  must19 Jan 15 at 14:06
    
Are you using a SP.FieldLookupValue? –  Hugh Wood Feb 17 at 9:12
    
Yes I tried this and the same error Even for an existing document, Accessing the lookup field value throw the same error. –  must19 Feb 17 at 21:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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