[英]Trying to add existing site column to the custom list using Visual Studio 2017 | (Sharepoint) [Microsoft.Sharepoint.Client]
[英]Is it possible to get the timezone of a Sharepoint site programmatically using Microsoft.SharePoint.Client?
是否可以使用C#中的Microsoft.SharePoint.Client以編程方式獲取Sharepoint網站的時區? 我需要Sharepoint網站時區以使其與特定時區匹配。
不幸的是,對於SharePoint的客戶端對象模型,無法獲得時區本身。 服務器對象模型和SPServices包含SPWeb的一個名為RegionalSettings的屬性,但是CSOM中缺少此屬性。
這里的好處是CSOM具有一項稱為FormatDateTime
的實用程序功能,您可以使用該功能將特定的字符串/日期轉換為站點的時區。
下面是在簡單的控制台應用程序中使用它的示例:
ClientContext clientContext = new ClientContext("http://yoursite.com");
Site site = clientContext.Site;
DateTime dt = DateTime.Parse("04/24/2013 5:44PM").ToUniversalTime();
ClientResult<string> cr = Utility.FormatDateTime(clientContext, clientContext.Web, dt, DateTimeFormat.DateTime);
clientContext.ExecuteQuery();
string value = cr.ToString();
DateTime webdt = DateTime.Parse(cr.Value.ToString());
Console.WriteLine(webdt.ToString());
Console.Read();
請注意,您需要在項目中包含SharePoint Client DLL並正確引用它們。 希望這可以幫助。
ClientContext context = new ClientContext(yourSite);
var culture = context.Web.RegionalSettings;
context.Load(culture);
var tz = culture.TimeZone;
context.Load(tz);
context.ExecuteQuery();
context.Dispose();
tz將包含您的時區,偏差等。
我的代碼類似於Deptor的代碼,但是我認為我的代碼解決了在SP 2013站點上嘗試運行它時遇到的一些語法問題。 我還使用一些全局變量來存儲諸如ClientContext,Web,Culture和Timezone SP CSOM對象之類的內容,以幫助學習者了解每個對象包含的內容。
碼:
var context, web, culture, tz;
var siteTzId, siteTzDesc, siteTzInformation;
getSiteRegionalTimeZone();
setTimeout(function(){
alert("Site TZ = |"+ siteTzDesc +"|");
}, 2000);
function getSiteRegionalTimeZone() {
context = new SP.ClientContext();
web = context.get_web();
culture = web.get_regionalSettings();
context.load(culture);
tz = culture.get_timeZone();
context.load(tz);
context.executeQueryAsync(function(){
siteTzId = tz.$5_0.$H_0.Id;
siteTzDesc = tz.$5_0.$H_0.Description;
siteTzInformation = tz.$5_0.$H_0.Information;
});
context.dispose();
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.