About Me

I am a Software Engineer presently working in Microsoft as vendor.The main aim of this blog is to Share my knowledge in Sharepoint,AZURE and Silverlight.

Sunday 27 July 2008

Sending Mail in Sharepoint using code.

we should include the following namespace
for this Add reference of Microsoft.Sharepoint in references.

using Microsoft.SharePoint.Utilities;

Get the header information which contains To,Subject,CC...

System.Collections.Specialized.StringDictionary headers = new System.Collections.Specialized.StringDictionary();
headers["To"] = v-sasikp@microsoft.com;
headers["Subject"] = "This is test mail";


finally,
SPUtility.SendEmail(myweb, headers, emailMsg, false);

But this requires configuration of email server in central administration.

Simple Ex:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace blogmail
{
class Program
{
static void Main(string[] args)
{
SPSite Site = new SPSite("http://sasikp-v:4008/");
SPWeb myweb = Site.OpenWeb();
SPUtility.SendEmail(myweb, false, false,"v-sasikp@microsoft.com", "This is test mail for blog","Pls dont reply to this mail this is testmail. ", false);

}
}
}

Thursday 10 July 2008

How to Read Url value.

Yesterday i m working on Url column of a list. My code has to read the Url value rather than Url.

and i wrote code simply like below.
foreach(Splistitem item in Allitems)
{
string urlvalue= item["Link"].tostring();
.......
}
as link is my column and item is splist item.

The above urlvalue is giving a url value which is converted to string.

to solve this i used SpFieldUrlValue.

SPFieldUrlValue linkField = new SPFieldUrlValue(item["Link"].tostring());
string linkname = linkField.Description;

Cheers i got the value finally.

Thursday 3 July 2008

How Dates Played with me.

1)Last night i was writing some code for my project and my requirement is to pull some data from the list based on some condition and condition is linked with some date field.

And i thought of using U2U tool which makes my life happy. But U2U is working fine, when i copied the query into my code its not working :( .

So rather than depending on U2U i went with CAML Language and Dates. I came to know that CAML Language recognises dates which is in "yyyy-MM-ddTHH:mm:ssZ" this format.

So lets coming to my code i have one date field in my list where i have to filter the list items based on the condition and condition is
" If the list fields date is less than Today-24 hrs " then get the items.

here i m deleting one day from today.
so the code i written is

DateTime today;
today = DateTime.Now;
Int32 DurationArtworkWFHrs = 24;
TimeSpan s = new TimeSpan(DurationArtworkWFHrs, 0, 0);
DateTime result = today.Subtract(s);
string date = result.ToString("yyyy-MM-ddTHH:mm:ssZ");

And my CAML Query looks like below :

SPQuery query = new SPQuery();
query.Query = "<-Where><-And><-Eq><-FieldRef Name='Status' /><-Value Type='Choice'>In Progress<-/Value><-/Eq><-Lt><-FieldRef Name='ProjectedEndDate' /><-Value Type='DateTime'>" +date+ "<-/Lt><-/And><-/Where>";

(Remove "-" from the above tags to get it worked).

SPListItemCollection myItems = mylist.GetItems(query);
int Projectcount = myItems.Count;

In the above code myitems collection gives the items i required. The variable date contains the converted string which our CAML Language understands. :)

2)One more Date Played with me in infopath form.

my requirement is i have to capture the date and it should be appended to the infopath form when it is submitted.

So for this i created one field in infopath form called "Millisec" .

and assigned our custom date format to this field.

XPathNavigator xnDoc = this.MainDataSource.CreateNavigator();
XPathNavigator fldMilliseconds;
fldMilliseconds = xnDoc.SelectSingleNode("/my:myFields/my:Millisec", this.NamespaceManager);
DateTime dd;
dd = DateTime.Now;
//string[] s = dd.GetDateTimeFormats();
//string date = s[105].ToString();
//date = date.Replace(":", ""); This works but we are capturing 135 elements into arraywhich is not required.
string date = dd.ToString("yyyy-MM-ddTHHmmss");
int TimeMilli = dd.Millisecond;
string instancename = date + TimeMilli;
fldMilliseconds.SetValue(instancename);

and while submitting the form append the value in the field. :)