CSC510250. Put SPFile.Exists into try ... catch block.

Description

Although it may seem intuitive that accessing the SPFile.Exists property would return True or False, in fact, if a file doesn’t exist, it throws an ArgumentException error.

Resolution

Put SPFile.Exists into try ... catch block.

private void DeactivateWeb(SPWeb web) 
{     
String defaultMasterUrl = "/_catalogs/masterpage/default.master";  
if (web.AllProperties.ContainsKey("OldMasterUrl"))     
{         
	string oldMasterUrl = web.AllProperties["OldMasterUrl"].ToString();         
	try         
	{             
		bool fileExists = web.GetFile(oldMasterUrl).Exists;             
		web.MasterUrl = oldMasterUrl;         
	}         
	catch (ArgumentException)         
	{             
		web.MasterUrl = defaultMasterUrl;         
	}  
	string oldCustomUrl = web.AllProperties["OldCustomMasterUrl"].ToString();         
	try         
	{             
		bool fileExists = web.GetFile(oldCustomUrl).Exists;             
		web.CustomMasterUrl = web.AllProperties["OldCustomMasterUrl"].ToString();         
	}         
	catch (ArgumentException)         
	{             
		web.CustomMasterUrl = defaultMasterUrl;         
	}  
	web.AllProperties.Remove("OldMasterUrl");         
	web.AllProperties.Remove("OldCustomMasterUrl"); 
}     
else      
{         
	web.MasterUrl = defaultMasterUrl;         
	web.CustomMasterUrl = defaultMasterUrl;     
} 
} 

Links

SPFile.Exists Property

Last edited Jul 30, 2014 at 1:15 PM by dvd73, version 3

Comments

No comments yet.