Sunday, May 3, 2009

How to Find Start Date and End Date of a Week in given Date

This is some thing i was really working hard on to find out. And it is a common thing we might need most of the time. Suppose we need to find the Start Date and End date of this week. Only thing I can reach programatically is Current Date. In C# it's

DateTime currentDate = DateTime.NOW;

Here is the code snippet to find the Start Date and Last Date of this week.

DateTime currentDate = DateTime.Now;
int week = myCal.GetWeekOfYear(currentDate , System.Globalization.DateTimeFormatInfo.CurrentInfo.CalendarWeekRule, DayOfWeek.Monday);
DateTime BeginingOfYear = new DateTime(DateTime.Now.Year, 1, 1);
System.Globalization.Calendar D = System.Globalization.CultureInfo.CurrentCulture.Calendar;
DateTime Weeks = D.AddWeeks(BeginingOfYear, Convert.ToInt32(week));

DateTime FirstDayOfWeek = Weeks.Subtract(new TimeSpan((int)Weeks.DayOfWeek, 0, 0, 0));

DateTime LastDayOfWeek = FirstDayOfWeek.AddDays(7);

Here We can Replace the current Date with the Date, Date that you have given.

Regards

Lakmal Kankanamge

How to Upload Files to SharePoint Doc Library...

I found this code snippet from a SharePoint Forum thread, It is about How to upload Files into SharePoint Document Library Using SharePoint API. Credit Should to original developer.
using (SPSite siteCollection = new
SPSite(url))

{
using (SPWeb spWeb = siteCollection.OpenWeb())

{

SPList spList = spWeb.GetList(url);


string fileName = "XXXX";

FileStream fileStream = null;
Byte[] fileContent = null;
try

{
string docPath = XXXX; //physical location of the file

fileStream = File.OpenRead(docPath + fileName);

fileContent = new
byte[Convert.ToInt32(fileStream.Length)];

fileStream.Read(fileContent, 0, Convert.ToInt32(fileStream.Length));

spList.RootFolder.Files.Add(spList.RootFolder.Url + "/" + fileName, fileContent, true);

spList.Update();

}
catch (Exception ex)

{

}
finally

{
if (fileStream != null)

{

fileStream.Close();

}

}

}
Regards

Lakmal Kankanamge.