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

context.sync hangs after getDescendants #5246

Open
bendecall opened this issue Jan 7, 2025 · 3 comments
Open

context.sync hangs after getDescendants #5246

bendecall opened this issue Jan 7, 2025 · 3 comments
Assignees
Labels
Area: Word Issue related to Word add-ins Needs: attention 👋 Waiting on Microsoft to provide feedback Platform: macOS

Comments

@bendecall
Copy link

Word context.sync() call freezes Word app

Your Environment

  • Platform: MacOS
  • Host: Word
  • Office version number: Version 16.92 (24120731)
  • Operating System: MacOS 15.0.1

Expected behavior

I'm trying to loop through all paragraph and print them in the console with their descendants.
In certain (even small) documents, context.sync() freezes after loading the result of getDescendants().

Current behavior

Word app hangs and needs to be killed. I've tried to use a timer (promise.race) to stop my function after a certain amount of times, but it won't work.
No errors are raised for try-catch

Steps to reproduce

Execute this function (my function might not be perfect, but it's strange that the whole app is crashing)


export async function processDocument() {
	await Word.run(async (context) => {
		try {
			// Load all paragraphs at once
			const paragraphs = context.document.body.paragraphs;
			context.load(paragraphs, ["items"]);
			await context.sync();

			// Load basic properties for all paragraphs
			paragraphs.items.forEach((item) => {
				context.load(item, ["text", "isListItem"]);
			});
			await context.sync();

			// Now that we have isListItem loaded, we can load list-specific properties
			const listItems = paragraphs.items.filter((item) => item.isListItem);
			listItems.forEach((item) => {
				context.load(item.listItem, ["listString"]);
			});
			await context.sync();

			// Process all list items
			for (const item of listItems) {
				// Print the list item and its text
				console.log(item.listItem.listString + " " + item.text);

				// Get the descendants of the list item and print them
				const descendants = item.listItem.getDescendants(true);
				if (descendants) {
					context.load(descendants, ["items"]);
					await context.sync(); // This call freezes at the first title with subheadings. Putting the call outside the for loop doesn't work either.

					descendants.items.forEach(async (descendant) => {
						context.load(descendant, ["text"]);
						await context.sync();
						console.log(descendant.text);
					});
				}
			}

			console.log("Done");
		} catch (error) {
			console.error(error);
			return error;
		}
	});
}

Example.docx

Link to live example(s)

See attached .docx file which is used for testing.

Provide additional details

I cannot find a way to trigger an error or kill context.sync() when it's taking too much time (crash of MS Word app).

Context

I'm trying to create a tree of a document structure and would like to understand which headings are ancestors or descendants of others.

Useful logs

See attached docx file I use as example

@microsoft-github-policy-service microsoft-github-policy-service bot added the Area: Word Issue related to Word add-ins label Jan 7, 2025
@microsoft-github-policy-service microsoft-github-policy-service bot added the Needs: attention 👋 Waiting on Microsoft to provide feedback label Jan 7, 2025
Copy link
Contributor

Thank you for letting us know about this issue. We will take a look shortly. Thanks.

@bendecall
Copy link
Author

Update: I tried the same function in Word Online and it won't crash, but will ignore the context.sync call.

RichApi.Error: The property ‘items’ is not available. Before reading the property value, call the load method on the container object and call ‘context.sync()’ on the associated request context.

@guoms1
Copy link

guoms1 commented Jan 8, 2025

Hi, @bendecall

Thank you for your report! We’ve noted your issue and are currently investigating. We appreciate your patience and will get back to you as soon as we have more information.

Thank you for your understanding!

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: attention 👋 Waiting on Microsoft to provide feedback Platform: macOS
Projects
None yet
Development

No branches or pull requests

2 participants