Below is the code to get the Daylight time .
Method
public static DateTime GetZoneBasedDateTime(TimezoneName zone)
{
try
{
string strzoneId = GetEnumStringValue(zone);
var dt = DateTime.UtcNow;
var tz = TimeZoneInfo.FindSystemTimeZoneById(strzoneId);
var utcOffset = new DateTimeOffset(dt, TimeSpan.Zero);
DateTime dtResult = utcOffset.ToOffset(tz.GetUtcOffset(utcOffset)).DateTime;
return dtResult;
}
catch (Exception ex)
{
WriteLogToFile("GetZoneTime(TimZoneName zone)", ex.Message, null, true);
return DateTime.MinValue;
}
}
}
Enum
public enum TimezoneName
{
[StringValueAttribute("Singapore Standard Time")]
Asia = 1,
[StringValueAttribute("W. Europe Standard Time")]
Geneva = 2,
[StringValueAttribute("Central Standard Time")]
Hoston = 3,
[StringValueAttribute("None")]
None = 0
}
Friday, October 18, 2013
Enum with String Value Tagging
I have an requirement where I need to get a string value from Enum like KeyValue mapping.
Below is the process to use Enum like a KeyValue mapping and get the keyvalue passing enum name .
using System.Reflection;
Declare a custom class.
[AttributeUsage( AttributeTargets.Field )]
public sealed class StringValueAttribute : Attribute
{
private string _value;
public StringValueAttribute(string value)
{
_value = value;
}
public string Value
{
get { return _value; }
}
}
Enum declaration.
public enum TimezoneName
{
[StringValueAttribute("Singapore Standard Time")]
Asia = 1,
[StringValueAttribute("W. Europe Standard Time")]
Geneva = 2,
[StringValueAttribute("Central Standard Time")]
Hoston = 3,
[StringValueAttribute("None")]
None = 0
}
Get string value from Enum.
public static string GetEnumStringValue(Enum evalue)
{
string output = null;
if (evalue != null)
{
Type type = evalue.GetType();
FieldInfo fi = type.GetField(evalue.ToString());
StringValueAttribute[] attrs =fi.GetCustomAttributes(typeof(StringValueAttribute),false) as StringValueAttribute[];
if (attrs.Length > 0)
{
output = attrs[0].Value;
}
}
return output;
}
Thursday, October 3, 2013
Write log for application
private void WriteLogToFile(string errorSource, string message, Exception exception, bool isNewLine)
{
try
{
//
FileStream fs = new FileStream(ConfigurationManager.AppSettings.Get("ErrorLog"), FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
StreamWriter errorLog = new StreamWriter(fs);
if (isNewLine)
errorLog.WriteLine("-".PadRight(100, '-'));
errorLog.WriteLine(DateTime.Now);
if (errorSource != null)
errorLog.WriteLine(errorSource);
if (message != null)
errorLog.WriteLine(message);
if (exception != null)
errorLog.WriteLine(exception);
errorLog.Close();
fs.Close();
fs.Dispose();
}
catch (Exception ex)
{
}
}
DateTime Filter in CAML
While filtering data on datetime field use SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.UtcNow) for sharepoint in CAML.
Friday, November 16, 2012
Create Newline in Infopath form
"?xml version="1.0" encoding="UTF-8"?>
?characters
cr="
"
lf="
"
crlf="
"
/">
Tuesday, November 13, 2012
Call WebService using Javascript
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="jquery.SPServices-0.6.1.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function()
{
});
//****************** Create a list item *******************//
function CreateNewItem()
{
var title=document.getElementById("txtTitle").value;
var batch =
"<Batch OnError=\"Continue\"> \
<Method ID=\"1\" Cmd=\"New\"> \
<Field Name=\"Title\">" + title + "</Field> \
</Method> \
</Batch>";
var soapEnv =
"<?xml version=\"1.0\" encoding=\"utf-8\"?> \
<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance/" \
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema/" \
xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope//"> \
<soap:Body> \
<UpdateListItems xmlns=\"http://schemas.microsoft.com/sharepoint/soap//"> \
<listName>TestList</listName> \
<updates> \
" + batch + "</updates> \
</UpdateListItems> \
</soap:Body> \
</soap:Envelope>";
$.ajax({
url: "http://sharepoint.server/sites/yoursite/_vti_bin/lists.asmx",
beforeSend: function(xhr) {
xhr.setRequestHeader("SOAPAction",
"http://schemas.microsoft.com/sharepoint/soap/UpdateListItems");
},
type: "POST",
dataType: "xml",
data: soapEnv,
complete: Result,
contentType: "text/xml; charset=utf-8"
});
}
function Result(xData, status)
{
}
</script>
Sunday, October 28, 2012
Check Postback in Javascript
function isPostBack() {
return document.referrer.indexOf(document.location.href) > -1;
}
return document.referrer.indexOf(document.location.href) > -1;
}
Subscribe to:
Posts (Atom)
