[mapguide-users] MapAgent SetResource in a batch

Mark Volz (LeapCAD) mark at leap-cad.com
Mon Sep 22 16:46:12 PDT 2014


Hi,

There are a couple of ways of doing this.

Below is some php code to get the definition of a layer, modify it and then
updated the new definition back into library. With a simple modification you
could change it so it reads the new definition from a file (just replace the
$updated_string with the xml string from the file).

You can run this code either from a web page or command prompt.

	$installDir ='C:\\Program Files\\OSGeo\\MapGuide\\';
	$extensionsDir = $installDir . 'Web\\www\\';
	$viewerDir = $extensionsDir . 'mapviewerphp\\';

	include $viewerDir . 'constants.php';
	$layer_path = 'Library://alayer.LayerDefinition';
	try 
	{
		//MgInitializeWebTier ($webconfigFilePath);
		MgInitializeWebTier($extensionsDir . "/webconfig.ini");

		//create a new site connection
		$siteConnection = new MgSiteConnection();//new MgSite();
		$siteConnection->Open(new
MgUserInformation('Administrator','admin'));
		
		//build a reference to the resource service
		$resourceService =
$siteConnection->CreateService(MgServiceType::ResourceService);

		//get the definition of the layer and create an xml object
from it
		$resId = new MgResourceIdentifier($layer_path);
		$byteReader = $resourceService->GetResourceContent($resId);
		$data_connection_XML = $byteReader->ToString();
		$doc = DOMDocument::loadXML($data_connection_XML);
		
		//modify the $doc variable as required i.e. find needle and
replace with haystack
		$updated_string= str_replace('needle' , 'haystack' , $doc);
		
		// create a new xml definition from the updated string
		$updated_doc = DOMDocument::loadXML($updated_string);
		$DataDefinition = $updated_doc->saveXML();
		
		//create the mgbyte source object
		$byteSource = new MgByteSource($DataDefinition,
strlen($DataDefinition));
		$byteSource->SetMimeType(MgMimeType::Xml);
		
		//save the updated layer definition 
		$newResId = new  MgResourceIdentifier($layer_path);
		$resourceService->SetResource($newResId,
$byteSource->GetReader(),  null);
		
	}
	catch (MgException $e)
	{
		echo "Mapguide ERROR: " . $e->GetMessage("eng") . "\n";
		echo $e->GetStackTrace("eng") . "\n";
		echo $e->GetDetails()."\n";
	}





More information about the mapguide-users mailing list