Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RichApi.Error: InvalidRequest on setting up field value #4050

Closed
ishanig11 opened this issue Jan 25, 2024 · 35 comments
Closed

RichApi.Error: InvalidRequest on setting up field value #4050

ishanig11 opened this issue Jan 25, 2024 · 35 comments
Assignees
Labels
Area: Word Issue related to Word add-ins Needs: author feedback Waiting for author (creator) of Issue to provide more info Resolution: has workaround Problem can be avoided by implementing workaround Status: no recent activity Issue or PR is stale (no recent activity)

Comments

@ishanig11
Copy link

I am using following code to set field values from Ms Word document

Word.run(async (context) => {
var customDocProps = context.document.properties.customProperties;
context.load(customDocProps);
return context.sync()
.then(function () {
// now load actual property
var filenameProp = customDocProps.getItemOrNullObject("PW", { matchPrefix: true });
context.load(filenameProp);
return context.sync()
.then(function () {
try{
var fields = context.document.body.fields.load("items");
fields.load(["code", "result"]);
return context.sync()
.then(function () {
if (fields.items.length > 0) {
fields.items.forEach((k,i)=> {
k.result.insertText(k.result.text, "Updated Field Value");
k.load(["code", "result"]);
const paragraph = context.document.body.insertParagraph("Hello World", Word.InsertLocation.start);
return context.sync().then(function () {
console.log("Updated Field");
});
});
}
});
} catch (error) {
console.log("Error: " + error);
}
});
});

await context.sync();
console.log("Items", customDocProps.items);
});

But this code returns following error message:
Uncaught (in promise) RichApi.Error: InvalidRequest
at new n (word-web-16.00.js:25:335398)
at o.processRequestExecutorResponseMessage (word-web-16.00.js:25:399844)
at word-web-16.00.js:25:397907

Your Environment

This is on Windows system.
Latest office js file from CDN

Expected behavior

Field value should get updated without any issue.

Current behavior

Getting Invalid request error

Steps to reproduce

  1. Call above code on any action in Word Document which has custom fields added through Quik Parts
  2. Check console logs
@microsoft-github-policy-service microsoft-github-policy-service bot added the Area: Word Issue related to Word add-ins label Jan 25, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added the Needs: attention 👋 Waiting on Microsoft to provide feedback label Jan 25, 2024
@yilin4
Copy link

yilin4 commented Jan 26, 2024

@ishanig11 Could you please provide your document and your complete gist? And I found there is some syntax errors in your code. There are some references of word js api as following.
customDocProps.getItemOrNullObject
https://learn.microsoft.com/en-us/javascript/api/word/word.custompropertycollection?view=word-js-preview#word-word-custompropertycollection-getitemornullobject-member(1)
the second parameter of k.result.insertText
https://learn.microsoft.com/en-us/javascript/api/word/word.range?view=word-js-preview#word-word-range-inserttext-member(1)

@ishanig11
Copy link
Author

FieldRefresh.docx
I dont see any syntax error in above code. I call this code inside one of function. And this function gets called as ExecuteAction of office manifest command.

@yilin4
Copy link

yilin4 commented Jan 29, 2024

@ishanig11 Can you provide your screenshots of the error?
image
I tried to run your code but the code with red underline is not correct. As the link I shared you before, the customDocProps.getItemOrNullObject doesn't have the second parameter and the insertText's second parameter should be one of these(insertLocation: Word.InsertLocation | "Replace" | "Start" | "End" | "Before" | "After")

@yilin4 yilin4 added the Needs: author feedback Waiting for author (creator) of Issue to provide more info label Jan 29, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added the Status: no recent activity Issue or PR is stale (no recent activity) label Feb 2, 2024
@ishanig11
Copy link
Author

Thanks for pointing this. I changed error line to k.result.text.replace(k.result.text, "New Value")
But this is not replacing field value. I want to update field value which user can do in Ms Word by "Update Field".

@microsoft-github-policy-service microsoft-github-policy-service bot removed Status: no recent activity Issue or PR is stale (no recent activity) Needs: author feedback Waiting for author (creator) of Issue to provide more info labels Feb 3, 2024
@yilin4
Copy link

yilin4 commented Feb 4, 2024

@ishanig11 It should be k.result.insert(k.result.text, "Replace"), you can try it. And if there still are questions on this please let me know~ thank you.

@ishanig11
Copy link
Author

DemoDocument.docx
image

Refer attached document. Here Hello Word is getting inserted. But you can see field values are not replace.

@yilin4
Copy link

yilin4 commented Feb 4, 2024

@ishanig11 Can you describe the scenario that you use field? From the document, I think you just want to replace the title and the major_rev to other string. If so, maybe the content control will be more suitable.

@ishanig11
Copy link
Author

ishanig11 commented Feb 4, 2024

Uploading ReactWordPortFolio.zip…
Steps.docx
Refer this document for exact requirement and current issue.
Uploading ReactWordPortFolio.zip…
Project file.

Kindly check and let me know in case of any questions.

@ishanig11
Copy link
Author

ishanig11 commented Feb 6, 2024

@yilin4 any update on this issue?

@yilin4
Copy link

yilin4 commented Feb 7, 2024

@ishanig11 Sorry for responding late. I have tried the following statement to insertText to a Field, you can have a try. But it's for changing the first Field in a document, and you can modify it according to your need.

const fields = context.document.body.fields; const field = fields.getFirstOrNullObject(); field.result.insertText("123", "Replace");

@ishanig11
Copy link
Author

@yilin4 : I tried above approach but still no change in document Quik Parts field value. Refer below screenshot:
image

@yilin4
Copy link

yilin4 commented Feb 7, 2024

@ishanig11 Sorry, I have tried it again, and found it just works in desktop. It has been put on our backlog<Bug#8727789> for internal track. We will keep track of this issue and let you know if there are any updates.

@ishanig11
Copy link
Author

@yilin4 : We are completely blocked due to this issue? Can you please talk with developer and find some workaround for now? This is blocker issue for us.

Thanks in advance.

@ishanig11
Copy link
Author

@yilin4 : I tried above approach but it did not work in Desktop version also. And I got below error:
image

@ishanig11
Copy link
Author

@yilin4 : any update? As mentioned earlier, please provide some workaround as mentioned earlier. As this is blocker issue for us.
Please take it on high priority.

Thanks in advance

@ishanig11
Copy link
Author

Any update?

@yilin4
Copy link

yilin4 commented Feb 18, 2024

@ishanig11 Sorry to respond late. It can work on desktop, but the field's type can't be others. You can try to set the field type to addin and have a try again.
About the online version, can using ContentControl meet your needs?
Inserting a ContentControl with tag in the selection range.
const selection = context.document.getSelection(); const cc = selection.insertContentControl(); cc.tag = "revision";
Getting the ContentControl with tag and changing its content.
const ccs = context.document.body.contentControls; const target = ccs.getByTag("revision"); target.getFirstOrNullObject().insertText("123", "Replace");

@ishanig11
Copy link
Author

We cant't use ContentControl in our document as we have older documents which are using Quik Parts. So need solution for Quik Parts refresh.

@yilin4
Copy link

yilin4 commented Feb 18, 2024

@ishanig11 Now, the online Field Object can't support the function you need. Could you estimate the user base and the revenue from your new add-in? So, we can better escalate this case and priories it.
And the current supported field type of online Word and desktop Word is like following:
image

@yilin4 yilin4 added the Needs: author feedback Waiting for author (creator) of Issue to provide more info label Feb 22, 2024
@ishanig11
Copy link
Author

@yilin4 : we have developed this feature and now will showcase these to our clients. As per our current surveys client will be interested in this feature. But right now I cant comment on user base and revenue. But definitely it will be good one.

Can you atleast tell me workaround to automatically refresh DOCPROPERTY field types in Desktop version for now?

@microsoft-github-policy-service microsoft-github-policy-service bot removed the Needs: author feedback Waiting for author (creator) of Issue to provide more info label Feb 22, 2024
@yilin4
Copy link

yilin4 commented Feb 22, 2024

@ishanig11 Can you tell me the specific field type that in your document that you want to refresh?

@yilin4 yilin4 added the Needs: author feedback Waiting for author (creator) of Issue to provide more info label Feb 22, 2024
@ishanig11
Copy link
Author

ishanig11 commented Feb 22, 2024 via email

@microsoft-github-policy-service microsoft-github-policy-service bot removed the Needs: author feedback Waiting for author (creator) of Issue to provide more info label Feb 22, 2024
@ishanig11
Copy link
Author

ishanig11 commented Feb 22, 2024

image: refer this image.
I want to refresh docproperty fields. These are fields basically from info->custom properties tab of word document.

@yilin4
Copy link

yilin4 commented Feb 23, 2024

@ishanig11 You can try this. It can work on desktop to refresh the docProperty field.
var fields = context.document.body.fields.load("items"); fields.load(["items"]); await context.sync(); for(let i = 0; i < fields.items.length; i++){ const field = fields.items[i]; field.load(); await context.sync(); if (field.type === Word.FieldType.docProperty){ field.result.insertText("test", "Replace"); } }

@ishanig11
Copy link
Author

With this field is not updated. See below image:
image

@yilin4
Copy link

yilin4 commented Feb 23, 2024

@ishanig11 I record this, you can see it. It's the desktop version.

Recording.2024-02-23.194129.mp4

@ishanig11
Copy link
Author

It worked for desktop. Thank you.

But need solution for online version also :) If you can provide some workaround for the same that would be really helpful.

@yilin4
Copy link

yilin4 commented Feb 23, 2024

@ishanig11 Now, the online version only has 4 field type, so if your document has the docProperty field, the online Word can't recognize it. If you have this need, we would like to suggest you go to https://techcommunity.microsoft.com/t5/microsoft-365-developer-platform/idb-p/Microsoft365DeveloperPlatform to ask a new question. Feature requests on it are considered when we go through our planning process.

@ishanig11
Copy link
Author

const ccs = context.document.body.contentControls; const target = ccs.getByTag("revision"); target.getFirstOrNullObject().insertText("123", "Replace");

If content control is marked as contents can not be edited then through program we can
t update value. I want to make these fields as read only for users so that they can't modify but should be able to update values programaically.
image

@yilin4
Copy link

yilin4 commented Mar 1, 2024

@ishanig11 There are two properties in the content control named cannotEdit and cannotDelete, I think you can use these two properties to control the content control not to be edited or deleted by users.

@zhenhuangMSFT zhenhuangMSFT added Needs: author feedback Waiting for author (creator) of Issue to provide more info Resolution: has workaround Problem can be avoided by implementing workaround and removed Needs: attention 👋 Waiting on Microsoft to provide feedback labels Mar 5, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added the Status: no recent activity Issue or PR is stale (no recent activity) label Mar 9, 2024
Copy link
Contributor

This issue has been automatically marked as stale because it is marked as needing author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. Thank you for your interest in Office Add-ins!

2 similar comments
Copy link
Contributor

This issue has been automatically marked as stale because it is marked as needing author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. Thank you for your interest in Office Add-ins!

Copy link
Contributor

This issue has been automatically marked as stale because it is marked as needing author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. Thank you for your interest in Office Add-ins!

Copy link
Contributor

This issue has been closed due to inactivity. Please comment if you still need assistance and we'll re-open the issue.

1 similar comment
Copy link
Contributor

This issue has been closed due to inactivity. Please comment if you still need assistance and we'll re-open the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Word Issue related to Word add-ins Needs: author feedback Waiting for author (creator) of Issue to provide more info Resolution: has workaround Problem can be avoided by implementing workaround Status: no recent activity Issue or PR is stale (no recent activity)
Projects
None yet
Development

No branches or pull requests

3 participants