Not Defteri

Akılda duracagına burda dursun

Working with SPFile in Sharepoint

SPFile class help us working with Sharepoint list attachments. I want to explain SPFile class with some console application. The first console application working with Personel List attachments and display Attachment full name and their file size;

using (SPSite site = new SPSite("http://testsite"))

{

 using (SPWeb web = site.OpenWeb())

  {                   

   SPList list = web.GetList("/Lists/Personel/");                   

   foreach(SPListItem item in list.Items)

    {

     foreach (string name in item.Attachments)

      {

       string fullname = item.Attachments.UrlPrefix + name;

       SPFile files = item.Web.GetFile(fullname);

       Console.WriteLine("File Name {0}, fileSize {1} kb",

       fullname, files.Length);   

      }                       

   }

  }

 }

}

 

This console application connect to http://testsite/ (This is my test site, change it your test enviromet) and get Personel list. Result of display attachment full path and their size like this.

 

Sample code : SPListAttachments.rar (349.27 kb)


Download A List Attachments Programatically
For example I want to download all attechments on personel list ( no shared documents). This code sample done it.

foreach (string name in item.Attachments)

 {

   string fullname = item.Attachments.UrlPrefix + name;

   SPFile files = item.Web.GetFile(fullname);

   Console.WriteLine("Downloded file: {0} ", name);

   byte[] binFile = files.OpenBinary();

   System.IO.FileStream fstream =

   System.IO.File.Create("c:\\Temp\\" + files.Name);

   fstream.Write(binFile, 0, binFile.Length);

 

  }   

 

Sample Code : SpDownloadAttachments.rar (349.85 kb)


Download Shared Document library Programatically

 

using (SPSite site = new SPSite("http://testsite"))

    {

       using (SPWeb web = site.OpenWeb())

         {

            SPList doclib = web.Lists["Documents"];

              foreach (SPListItem item in doclib.Items)

                {

                  byte[] binFile = item.File.OpenBinary();

                  System.IO.FileStream fstream =

                  System.IO.File.Create("c:\\Temp\\" + item.File.Name);

                  fstream.Write(binFile, 0, binFile.Length);

                 }

         }

    }

Sample Code : SPDownloadDocuments.rar (349.76 kb)

Posted: Apr 26 2010, 01:02 by kkaradag | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: SharePoint 2007

Difference between SpListItem.Update and SpListItem.SystemUpdate

To update a list item we have two ways. One of them is SPListitem.Update and its is most popular way. But this method is change version history and trigger all alerts. Some times, some reason you can want to avoid change version history or triggering allerts. Like this case, SpListitem.SystemUpdate medhod help us. Because that SystemUpdate() is not changing version history.

For Example; I have a list name Personel and I want to change secont record by a console application with SPitem.Update method. Before Run this code sample my list item history like this.

 using (SPSite site = new SPSite("http://testsite"))

            {

                using (SPWeb web = site.OpenWeb())

                {

                    SPList list = web.Lists["Personel"];

                    SPListItem item = list.Items[1];

                    item["Title"] = "Sample Update with Versions.";

                    item.Update();

                    list.Update();

                }

            }

After work this Consle Application secont item version history changin like this;

The outher mathod which SystemUpdate() don't add new version history.

using (SPSite site = new SPSite("http://testsite"))

            {

                using (SPWeb web = site.OpenWeb())

                {

                    SPList list = web.Lists["Personel"];

                    SPListItem item = list.Items[1];

                    item["Title"] = "Sample Update with out add Versions";

                    item.SystemUpdate();

                    list.Update();

                }

            }

Finnaly My item history changing but not a new version number.

Download Sample Code : SPSystemUpdate.rar (348.76 kb)

Posted: Apr 25 2010, 22:31 by kkaradag | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: SharePoint 2007

Bind the List Data to a DataGird Control

To Bind the List Data to a DataGrid Control, you can write like this example webpart. This web part connect to Personel List and convert  List data to DataTable Object.

        protected override void CreateChildControls()

        {

            base.CreateChildControls();

 

            SPWeb site = SPControl.GetContextWeb(Context);

            SPList List = site.Lists["Personel"];

            SPListItemCollection  Listitems = List.Items;

            DataTable MyTable = Listitems.GetDataTable();

 

            DataGrid MyGrid = new DataGrid();

            MyGrid.DataSource = MyTable;

            MyGrid.DataBind();

            this.Controls.Add(MyGrid); 

        }

After the publish this webpart your test site via Visual Studio. it's avalibel to add as webpart like this.

 

To Download This sample application Click  BindTheListData.rar (358.57 kb)

Posted: Apr 25 2010, 22:12 by kkaradag | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: SharePoint 2007

Writing CAML Queries

The Collaborative Application Markup Language (CAML) is an XML-based query language that helps you querying and customizing Web sites based on Windows SharePoint Services. CAML Quesies has a root element named Query and Query element has two optional elements named Order by elemet and Where elemet.

<Query>

  <OrderBy>

      <!-- Query elemend to sort result data-->

  </OrderBy>

  <Where>

      <!--Query elemet to filter data-->

  </Where>

</Query>

Order By Element

Order by elemet use to sort resutl data for example, You create a Microsoft Windows SharePoint Service list named Personel. And you want to sort of data by FirstName column. To do this you can write like this CAML Query.

  <OrderBy>

       <FieldRef Name='FirstName' />

  </OrderBy

The order by element has one or more fields which you want to sort. The other detail about order by is Ascending or Descending. İf you want to sort data Descending, You must change your query <FieldRef Name='FirstName' Ascending='False' /> because that Ascending is a default key and value is True.

Where Element

The where element is the other part of CAML Queries. Where element help us filtering data.  For ex. I want to filter list data by LastName = Kemal.

<Where>

  <Eq>

    <FieldRef Name='LastName' />

    <Value Type='Text'>Kemal</Value>

  </Eq>

</Where>

And tihs example I want to add the other criteria which City = Istanbul to to this change the query a bit and add a new key <And>.

<Where>

  <And>

    <Eq>

      <FieldRef Name='LastName' />

      <Value Type='Text'>Kemal</Value>

    </Eq>

    <Eq>

      <FieldRef Name='City' />

      <Value Type='Text'>İstanbul</Value>

    </Eq>

  </And>

</Where>

The CAML Query Operators

 

Eq  Equals  == 
Neq  Not  Equals   !=
Gt Greater than   >
Geq  Greater than or equal   >=
Lt  Lower than   <
Leq  Lower than or equal   <=
IsNull  Is Null   
BeginsWith   Begins With  
Contains     

Posted: Apr 24 2010, 03:02 by kkaradag | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: SharePoint 2007

Sharepoint 2007 and Site Defination

Create Custome Site Defination

  1. Navigate to C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\SiteTemplates
  2. Copy STS folder ,past it some place and rename new folder for ex. BlackBird
  3. Double click to BlackBird Folder and open default.aspx  with notpad or other tools.
  4. Edit some imformation on default.aspx for example I added new <h2> line to PlaceHolderMain zone.
  5. Edit ONET.XML that placed XML folder some place.
  6. Replace all STS expression to BlackBird.
  7. Navigate to C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\1033\XML
  8. Copy WEBTEMP.XML and past some place and rename it as WEBTEMPBlackBird.XML
  9. Edit WEBTEMPBlackBird.XML and replace it like this

<?xml version="1.0" encoding="utf-8"?>

<!-- _lcid="1033" _version="12.0.4518" _dal="1" -->

<!-- _LocalBinding -->

<Templates xmlns:ows="Microsoft SharePoint">

 <Template Name="BlackBird" ID="10009">

    <Configuration ID="1"

     Title="BlackBird template"

     Hidden="FALSE" ImageUrl="/_layouts/1033/images/BlackBird.PNG"

     Description="A BlackBird template."

     DisplayCategory="Custom"

     AllowGlobalFeatureAssociations="False" >    </Configuration>

 </Template>

</Templates>

After complate all setps you must two things. One of them is copy yor image file which you declared in WEBTEMPBlackBird.XML to C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\LAYOUTS\1033\IMAGES  folder. The finaly Run iisreset command on command promt( if you are working on a farm, you must all steps on all servers)

Now, Navigate on Sharepoint Central Admin, Application Managemet | Create Site Collection Your site template is ready under the custum templates like this.

Posted: Apr 13 2010, 21:24 by kkaradag | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: SharePoint 2007

Developing Content Types with Visual Studio 2008

To create a Content Type with visual Studio as a solution follow this steps;

1) Open Visual Studio 2008 and Create an New Project onder Visual C# - SharePoint.

2) Select empty project tempate.

3) Fill Project name and Location information that you want And make sure "create directory for solution" check box is cheked. So click OK button.

4)After complate project creating progress, Visual Studio repaired a kind of directory for your using like this.

5) Right Click to project and Click Add --> Add New item and select Content Type onder the Sharepoint item templates.

6) Visual Studio ask you that Content Type you want to use. Select Event Type Content Type and check "Add With Event Receiver"

7) After Complate this step visual studio repaired for you a spacial folder and 3 files. And doble click to CustomeCalendarContentType.xml file

8) Under the </ContentType> You can see "<!--" and unmark this section and replace it with folowing code

<Field ID="Your Uniq Code here"

Name="Region" Group="My Model"

Type="Choice"

DisplayName="Region"

SourceID="http://schemas.microsoft.com/sharepoint/v3/fields"

StaticName="Region"

Description=""

Sealed="True"

Required="True"

AllowDeletion="False"

Format="Dropdown"

FillInChoice="False">

<CHOICES><

CHOICE>Location 1</CHOICE>

<CHOICE>Location 2</CHOICE><

CHOICE>Location 3</CHOICE>

</CHOICES><

Default>Location 1</Default>

</Field>

9) Go to the Tools menu Click Create GUI nad creat a new GUID and copy that and pat "your uniq code here" section.

10) on the xml file go to the file referance section and add following code with same uniq GUI like this. Finnal xml file must be like this;

<?xml version="1.0" encoding="utf-8"?> <Elements Id="19d553bd-b57e-4815-a7d1-76be664b2a78" xmlns="http://schemas.microsoft.com/sharepoint/">

<ContentType ID="0x01020009b4f3550baa4fed9de1a2479c83b9a1"

Name="CustomeCalendarContentType"

Group="Development"

Description="Developing Content Type"

Version="0"><FieldRefs>

 

<FieldRef ID="{D3376E75-AB45-4e00-B9DE-687A5761040B}"

Name="Region" />

 

</
FieldRefs>

</ContentType><

Field ID="{D3376E75-AB45-4e00-B9DE-687A5761040B}" Name="Region" Group="My Metadata Model" Type="Choice" DisplayName="Region" SourceID="http://schemas.microsoft.com/sharepoint/v3/fields" StaticName="Region" Description="" Sealed="True" Required="True" AllowDeletion="False" Format="Dropdown" FillInChoice="False">

<CHOICES><

CHOICE>Amerika</CHOICE>

<CHOICE>Avrupa</CHOICE><

CHOICE>Asya</CHOICE>

</CHOICES><

Default>Avrupa</Default> </Field>

</Elements>

11)Right click your project and select deploy.

12) Go to the your site and navigate site Action --> Site Settings.

13)under the site Content Types, you can see your custome content type like this.

14) Navigate Site Action --> Create and Select Celendar template and create new Celendar. And you can use this new content Types for this celendar.

 

Posted: Apr 13 2010, 02:41 by kkaradag | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under:

Deployment Fail from Visual Studio to SharePoint

I have just install visual studio 2008 on my enviroment with sharepoint extensions. But my operation system is x64 and when I developed a sharepoint application and deploy it on Visual Studio, I take a deployment faid with this message;

 2009/04/02 08:55:56    Error
Error: System.ServiceModel.ProtocolException
System.ServiceModel.ProtocolException: The content type text/html; charset=utf-8 of the response message does not match the content type of the binding (text/xml; charset=utf-8). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly. The first 1024 bytes of the response were: '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>IIS 7.0 Detailed Error - 500.19 - Internal Server Error</title>
<style type="text/css">
<!--
body{margin:0;font-size:.7em;font-family:Verdana,Arial,Helvetica,sans-serif;background:#CBE1EF;}
code{margin:0;color:#006600;font-size:1.1em;font-weight:bold;}
.config_source code{font-size:.8em;color:#000000;}
pre{margin:0;font-size:1.4em;word-wrap:break-word;}
ul,ol{margin:10px 0 10px 40px;}

The problem is X64 problem and solving is very easy. Run a Cmd and navigate this address "C:\Windows\Microsoft.NET\Framework64\v3.0\Windows Communication Foundation"   and run this command;  ServiceModelReg.exe -i  after complate execution deployment works well.

 

 

 

 

Posted: Apr 13 2010, 01:54 by kkaradag | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: SharePoint 2007