SOLVED

Calling _api/search/postquery from Modern Web Part (security validation invalid)

%3CLINGO-SUB%20id%3D%22lingo-sub-88946%22%20slang%3D%22en-US%22%3ECalling%20_api%2Fsearch%2Fpostquery%20from%20Modern%20Web%20Part%20(security%20validation%20invalid)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-88946%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20trying%20to%20call%20%2F_api%2F_search%2Fpostquery%20from%20a%20modern%20Script%20Editor%20web%20part%2C%20and%20I%20am%20getting%20this%20error%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EVM6497%3A86%20%7B%22error%22%3A%7B%22code%22%3A%22-2130575251%2C%20Microsoft.SharePoint.SPException%22%2C%22message%22%3A%7B%22lang%22%3A%22en-US%22%2C%22value%22%3A%22The%20security%20validation%20for%20this%20page%20is%20invalid%20and%20might%20be%20corrupted.%20Please%20use%20your%20web%20browser's%20Back%20button%20to%20try%20your%20operation%20again.%22%7D%7D%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20same%20code%20works%20fine%20in%20classic%20sharepoint%20web%20part%2C%20is%20there%20something%20additional%20I%20have%20to%20account%20for%20in%20a%20simple%20REST%20API%20call%20in%20modern%3F%20%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%20%20var%20postData%20%3D%20%7B%0A%20%20%20'request'%3A%7B%0A%20%20%20'__metadata'%20%3A%20%7B'type'%20%3A%20'Microsoft.Office.Server.Search.REST.SearchRequest'%7D%2C%0A%20%20%20'Querytext'%20%3A%20%22*%22%2C%0A%20%20%7D%7D%3B%20%0A%20%20%0A%20%20%24.ajax(%7B%0A%20%20%20type%3A%20%22POST%22%2C%0A%20%20%20url%20%3A%20%22https%3A%2F%2Fmytenant.sharepoint.com%2F_api%2Fsearch%2Fpostquery%22%2C%20%20%20%20%0A%20%20%20data%3A%20JSON.stringify(postData)%2C%0A%20%20%20headers%3A%20%7B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%22accept%22%3A%20%22application%2Fjson%3Bodata%3Dverbose%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%22content-type%22%3A%20%22application%2Fjson%3Bodata%3Dverbose%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%22X-RequestDigest%22%3A%20%24(%22%23__REQUESTDIGEST%22).val()%0A%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20success%20%3A%20function%20onSuccess(data%2C%20request)%7B%0A%20%20%20%20console.log(data)%3B%0A%20%20%20%7D%2C%0A%20%20%20error%20%3A%20function%20onError(error)%20%7B%0A%20%20%20%20console.log(error.responseText)%3B%20%20%20%20%20%0A%20%20%20%7D%0A%20%20%7D)%3B%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-88946%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-89138%22%20slang%3D%22en-US%22%3ERE%3A%20Calling%20_api%2Fsearch%2Fpostquery%20from%20Modern%20Web%20Part%20(security%20validation%20invalid)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-89138%22%20slang%3D%22en-US%22%3EWell%2C%20magically%2C%20I%20come%20in%20today%2C%20and%20now%20this%20is%20working%20as%20it%20should.%20Not%20sure%20why%20it%20wouldn't%20work%20yesterday.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-88975%22%20slang%3D%22en-US%22%3ERe%3A%20Calling%20_api%2Fsearch%2Fpostquery%20from%20Modern%20Web%20Part%20(security%20validation%20invalid)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-88975%22%20slang%3D%22en-US%22%3EAttempting%20that%20currently%20with%20no%20luck%20%3A(%3C%2Fimg%3E%3CBR%20%2F%3E%3CBR%20%2F%3EThe%20RequestDigest%20from%20the%20_api%2Fcontextinfo%20and%20the%20POST%20are%20on%20the%20same%20site%2C%20and%20I%20get%20the%20same%20error.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-88974%22%20slang%3D%22en-US%22%3ERe%3A%20Calling%20_api%2Fsearch%2Fpostquery%20from%20Modern%20Web%20Part%20(security%20validation%20invalid)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-88974%22%20slang%3D%22en-US%22%3E%3CP%3EI've%20found%20that%20if%20I%20make%20POSTs%20from%20a%20different%20site%20(or%20site%20collection)%20than%20the%20REST%20URL%20target%2C%20my%20on%20page%20RequestDigest%20won't%20do.%20I%20need%20to%20grab%20a%20fresh%20one%20from%20the%20target%20site%20_api%2Fcontextinfo.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENot%20sure%20if%20this%20is%20your%20issue%2C%20but%20worth%20a%20try.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Valued Contributor

I am trying to call /_api/_search/postquery from a modern Script Editor web part, and I am getting this error:

 

VM6497:86 {"error":{"code":"-2130575251, Microsoft.SharePoint.SPException","message":{"lang":"en-US","value":"The security validation for this page is invalid and might be corrupted. Please use your web browser's Back button to try your operation again."}}}

 

This same code works fine in classic sharepoint web part, is there something additional I have to account for in a simple REST API call in modern?  

 

		var postData = {
			'request':{
			'__metadata' : {'type' : 'Microsoft.Office.Server.Search.REST.SearchRequest'},
			'Querytext' : "*",
		}}; 
		
		$.ajax({
			type: "POST",
			url : "https://mytenant.sharepoint.com/_api/search/postquery",    
			data: JSON.stringify(postData),
			headers: { 
	            "accept": "application/json;odata=verbose",
	            "content-type": "application/json;odata=verbose"
	            "X-RequestDigest": $("#__REQUESTDIGEST").val()
	        },
			success : function onSuccess(data, request){
				console.log(data);
			},
			error : function onError(error) {
				console.log(error.responseText);					
			}
		});
3 Replies
Highlighted
Solution

I've found that if I make POSTs from a different site (or site collection) than the REST URL target, my on page RequestDigest won't do. I need to grab a fresh one from the target site _api/contextinfo.

 

Not sure if this is your issue, but worth a try.

Highlighted
Attempting that currently with no luck :(

The RequestDigest from the _api/contextinfo and the POST are on the same site, and I get the same error.
Highlighted
Well, magically, I come in today, and now this is working as it should. Not sure why it wouldn't work yesterday.