Search code examples
web-servicessharepointtimezonewindows-sharepoint-serviceslocale

Is there a way to get a Sharepoint site's locale with web services?


I've looked through the wss 3.0 documentation, but I can't find anything. I would like to retrieve the locale for a Sharepoint 2007 site using web services (no access to server so can't deploy anything on it), so as to know what time zone is site the configured in. Is that possible?

thx


Solution

  • Ok, here's what I did to solve this:

    • make a query to the list for 1 element, getting times set for site's locale
    • make a query for that element, getting times in UTC
    • calculate the difference between the 2 results.

    Code:

        /// <summary>
        /// Gets the difference between local time and UTC to calculate offset.
        /// Makes a query to the list that returns 1 element with times using the locale as set in the site's settings.
        /// Then makes another query for the same element with UTC times to calculate the difference.
        /// </summary>
        /// <param name="list">list to query</param>
        /// <param name="client">WS object</param>
        /// <returns>Time offset as TimeSpan object</returns>
        private TimeSpan getSiteTZOffset(List list, WSLists.Lists client )
        {
            //Set up query parameters
            XmlDocument xmlDoc = new XmlDocument();
            XmlNode emptyQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");
            XmlNode queryWithID = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");
    
            XmlNode ndOptions = xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", "");
            ndOptions.InnerXml = "<DateInUtc>True</DateInUtc>";
    
            XmlNamespaceManager xnm = new XmlNamespaceManager(xmlDoc.NameTable);
            xnm.AddNamespace("z", "#RowsetSchema");
    
            // Gets the attribute that serves as modified date
            MapAttribute modifiedDateAttr = attributes.Single(x => x.Value.DateTimeModifiedField).Value;
            // Gets the Id attribute
            MapAttribute idAttr = attributes.Single(x => x.Value.KeyReference).Value;
    
            //Get 1 result with site's local time
            XmlNode resLocalTime = client.GetListItems(list.ListID, list.ViewID, emptyQuery, null, "1", null, null);
            XmlNodeList itemsLocalTime = resLocalTime.SelectNodes("//z:row", xnm);
    
            // 2nd query filters on ID of the item returned by 1st query
            queryWithID.InnerXml = string.Format("<Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>{0}</Value></Eq></Where>",
                itemsLocalTime[0].Attributes[idAttr.Name].Value);
    
            //get the result with UTC time
            XmlNode resUtc = client.GetListItems(list.ListID, list.ViewID, queryWithID, null, "1", ndOptions, null);
            XmlNodeList itemsUtc = resUtc.SelectNodes("//z:row", xnm);
    
            //Converts string values to DateTime objects
            DateTime localTime = DateTime.Parse(itemsLocalTime[0].Attributes[modifiedDateAttr.Name].Value);
            DateTime utcTime = getUtcTime(itemsUtc[0].Attributes[modifiedDateAttr.Name].Value);
            // Gets offset
            TimeSpan offset = localTime - utcTime;
    
            return offset;
        }