In my timer job code, there is a section where for each site collection in the web application, I am reading an XML template file in the style library of that site collection.

The code looks something like

var xmlDoc = new XmlDocument();
xmlDoc.Load(site.Url + "/style%20library/xml/test-template.xml");

The error I'm getting is

The remote server returned an error: (401) Unauthorized.

What would I need so that my timer job has access to this file in the style library?

The account used on execution is SHAREPOINT\SYSTEM by the way.
site.Url resolves to http://mySpSite/siteCollection/

  • 1
    which user are you using on your Timer Job service? Did you grant access to that use to the web site where you trying to read your xml? Mar 24, 2014 at 16:50
  • I'm not doing anything special and it should just be the default Farm service account. Do I still need to explicitly grant permission for it in the site/style library?
    – kei
    Mar 24, 2014 at 17:02
  • is it the farm account or it should be? Because if it IS the current farm account then is strange if it's not then that is why u got an Unauthorized error Mar 24, 2014 at 18:22

1 Answer 1


I finally figured it out! For some reason I was unable to use xmlDoc.Load(string URL) but I was able to workaround it using xmlDoc.Load(Stream stream)

Here's what worked for me:

SPList styleLib = site.RootWeb.Lists.TryGetList("Style Library");
SPFolder templatesFolder = styleLib.RootFolder.SubFolders["xml"];
SPQuery query = new SPQuery
    Query = @"<Where>
                    <FieldRef Name=""Title"" />
                    <Value Type=""Text"">test-template.xml</Value>
    RowLimit = 1,
    Folder = templatesFolder
SPListItemCollection templates = styleLib.GetItems(query);
SPFile mailTemplateFile = templates[0].File;

XmlDocument xmlDoc = new XmlDocument();
byte[] bytes = mailTemplateFile.OpenBinary();
Stream stream = new MemoryStream(bytes);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.