Friday, April 10, 2009

Download Document from SharePoint Library using WebService

In  following code, I am reading the contents from the documents and creating and storing documents in local system

//Copy WebService Settings
string webUrl = "http://portal/";
WSCopy.Copy copyService = new WSCopy.Copy();

copyService.Url = webUrl+"/_vti_bin/copy.asmx";

copyService.Credentials = System.Net.CredentialCache.DefaultCredentials;

//Source and Destination Document URLs
string sourceUrl = "http://localhost:1000/Shared%20Documents/Sample.doc";
string destinationUrl = "C:\\Documents\Sample.doc";


//Variables for Reading metadata’s of a document
WSCopy.FieldInformation fieldInfo = new WSCopy.FieldInformation();
WSCopy.FieldInformation[] fieldInfoArray = { fieldInfo };
WSCopy.CopyResult cResult1 = new WSCopy.CopyResult();
WSCopy.CopyResult cResult2 = new WSCopy.CopyResult();
WSCopy.CopyResult[] cResultArray = { cResult1, cResult2 };


//Receive a Document Contents into Byte array (filecontents)
byte[] fileContents = new Byte[4096];
copyService.GetItem(sourceUrl, out fieldInfoArray, out fileContents);


//Create a new file and write contents to that document
FileStream fStream = new FileStream(destinationUrl, FileMode.Create, FileAccess.ReadWrite);
fStream.Write(fileContents, 0, fileContents.Length);
fStream.Close();

Thursday, April 9, 2009

SHAREPOINT DESIGNER 2007 IS NOW FREE!

SharePoint Designer 2007 provides the powerful tools you need to deliver compelling and attractive SharePoint sites and quickly build workflow-enabled applications and reporting tools on the SharePoint platform, all in an IT-managed environment.

You can download it at http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=baa3ad86-bfc1-4bd4-9812-d9e710d44f42.

Saturday, April 4, 2009

How to delete data from Sharepoint List using API?

Normally once you delete the data from the list it goes to Recycle Bin. During reading SDK I came to know about a method  'Delete' which deletes data permanently, does not send it recycle bin. So now we have two different approach for deleting list data.

Approach 1 - Delete Data Permanently

using (SPSite siteCollection = new SPSite("http://portal/"))
{
    using (SPWeb site = siteCollection.OpenWeb())
    {
       SPList list1 = site.Lists["MyList"];
       for (int i = list1.Items.Count - 1; i >= 0; i--)
      {
          list1.Items.Delete(i);
      }
    }
}

Approach 2 - Delete Data and it goes to recycle bin

using (SPSite siteCollection = new SPSite("http://portal/"))
{
   using (SPWeb site = siteCollection.OpenWeb())
   {
   SPList list1 = site.Lists["MyList"];
   for (int i = list11.Items.Count - 1; i >= 0; i--)
   {
      SPListItem olistitem = list11.Items[i];
      olistitem.Recycle();
   }
   }
}

Thursday, March 26, 2009

Common Date Time formulas for Sharepoint

Get Week of the year

=DATE(YEAR([Start Time]),MONTH([Start Time]),DAY([Start Time]))+0.5-WEEKDAY(DATE(YEAR([Start Time]),MONTH([Start Time]),DAY([Start Time])),2)+1

First day of the week for a given date:

=[Start Date]-WEEKDAY([Start Date])+1

Last day of the week for a given date:

=[End Date]+7-WEEKDAY([End Date])

If you want your week to start from Monday to Sunday, include an extra parameter value to your WEEKDAY() function

First day of the week for a given date:

=[Start Date]-WEEKDAY([Start Date],2)+1

Last day of the week for a given date:

=[End Date]+7-WEEKDAY([End Date],2)
First day of the month for a given date:

=DATEVALUE(“1/”&MONTH([Start Date])&”/”&YEAR([Start Date]))

TODAY + 1 hour formula
You can use formula like (1/24th of a day or 1 hour to the rest of us is 0.04167!)


=[Created]+0.04167

But we can’t use the Created/Modified field in the formula for a new record because it doesn’t exist yet.


OK, so what if we try and use this in the Default Value column as
=Today+0.04167

This will always be ‘Today at 1AM’ rather than ‘Today in exactly 1 hour’ as Today uses 12:00 AM as the time offset. Unfortunately there is no [Now] function in SharePoint.

Last day of the month for a given year (does not handle Feb 29). Result is in date format:

=DATEVALUE (CHOOSE(MONTH([End Date]),31,28,31,30,31,30,31,31,30,31,30,31) &”/” & MONTH([End Date])&”/”&YEAR([End Date]))

Day Name of the week : e.g Monday, Mon

=TEXT(WEEKDAY([Start Date]), “dddd”)

=TEXT(WEEKDAY([Start Date]), “ddd”)

The name of the month for a given date – numbered for sorting – e.g. 01. January:

=CHOOSE(MONTH([Date Created]),”01. January”, “02. February”, “03. March”, “04. April”, “05. May” , “06. June” , “07. July” , “08. August” , “09. September” , “10. October” , “11. November” , “12. December”)


Get Hours difference between two Date-Time :

=IF(NOT(ISBLANK([End Time])),([End Time]-[Start Time])*24,0)
Date Difference in days – Hours – Min format : e.g 4days 5hours 10min :

=YEAR(Today)-YEAR(Created)-IF(OR(MONTH(Today)<MONTH(Created),AND(MONTH(Today)=MONTH(Created),DAY(Today)<DAY(Created))),1,0)&” years, “&MONTH(Today)-MONTH(Created)+IF(AND(MONTH(Today)<=MONTH(Created),DAY(Today)<DAY(Created)),11,IF(AND(MONTH(Today)<MONTH(Created),DAY(Today)>=DAY(Created)),12,IF(AND(MONTH(Today)>MONTH(Created),DAY(Today)<DAY(Created)),-1)))&” months,“&Today-DATE(YEAR(Today),MONTH(Today)-IF(DAY(Today)<DAY(Created),1,0),DAY(Created))&” days”
You can get Get more formulas from

http://office.microsoft.com/en-us/sharepointtechnology/HA011609471033.aspx

Tuesday, March 17, 2009

RunWithElevatedPrivileges is not working - invalid/wrong security context

To fix this you need to get a news ite reference

SPSecurity.RunWithElevatedPrivileges(delegate()

{

//Gets a new security context using SHAREPOINT\system
//or by using (SPSite oSite = new SPSite(this.Page.Request.Url.ToString()))

using(SPSite oSite = new SPSite(SPContext.Current.Site.ID))
{
   using (SPWeb oWeb = oSite.OpenWeb())
   {
    oWeb.AllowUnsafeUpdates = true;
    // Add a record to the new list
    SPList oList = oWeb.Lists["myListName"];
    if (oList != null)
   {
    SPListItem item = oList.Items.Add();
    item["Title"] = "new item";

    // item.update to committ
    item.Update();
    }
 }
}
});