i'm writing event receiver sharepoint site, , want receiver edit contents of basic page after created. here function giving me issues:
public void fillpage(spsite site, spitemeventproperties properties, string pagename) { using (site) { // wait until page has been generated while (!pageexists(properties.beforeurl)) { thread.sleep(10000); } thread.sleep(30000); // added can check url exists in browser spweb web = site.rootweb; spfile page = web.getfile(properties.beforeurl); page.checkout(); // throws spexception: 'url invalid'. ... } } the pageexists function uses httpwebrequest pointed page generated:
public bool pageexists(string url_ending) { httpwebrequest request = (httpwebrequest)webrequest.create(new uri((the root site url) + url_ending)); request.timeout = 15000; try { httpwebresponse response = (httpwebresponse)request.getresponse(); return true; } catch (webexception we) { if (we.message.contains("unauthorized")) { return true; // if it's authorization error, page exists access denied } return false; } } the checkout function returns: "spexception: url '...' invalid. may refer nonexistent file or folder, or refer valid file or folder not in current web." in addition, added breakpoint @ line containing 'page.checkout()' , examined page variable, , found of members throw 'system.io.filenotfoundexception' or 'system.indexoutofrangeexception', though it's pointed correct url. i've checked httpwebrequest pointed correct url, is, , mentioned in comments check see page exists in browser before code can attempt check out.
from searching did, found out error thrown when database logs filling up. found, in case error occur when attempting check out documents sharepoint site itself, , have not had issue; error when attempt check out page event receiver. idea what's going on?
i found article
http://blog.mastykarz.nl/inconvenient-spwebgetfilestring/
which explains getfile can have unexpected results.
there workaround provided:
using (spsite site = new spsite("http://moss")) { using (spweb web = site.rootweb) { object o = web.getfileorfolderobject("/site/subsite1/pages/default.aspx"); if (o spfile) { spfile f = (spfile)o; } } } you should give try !
Comments
Post a Comment