Skip to main content

Le Blog de [Jonathan Martiat]'s Blog

Go Search
Le Blog de [Jonathan Martiat]'s Blog
  

Select
Normal
EnglishUse SHIFT+ENTER to open the menu (new window).
Normal
FrançaisUse SHIFT+ENTER to open the menu (new window).
Categories
MOSS 2007
WSS 3.0
View Jonathan Martiat's profile on LinkedIn
How to upload a file to a document library in SharePoint

In this exemple, I will show you how to upload a file to a document library and how to link information to the uploaded file.

According that we have an <asp:FileUpload ID="FileUpload1" runat="server" /> in an aspx page that contains the file path that we want to upload.

1. Open a stream and save the file to an array of bytes.

Stream myStream = FileUpload1.PostedFile.inputStream;

byte[] bytes = new byte[Convert.ToInt32(FileUpload1.PostedFile.ContentLength)];

myStream.Read(bytes, 0, byt.Length);

myStream.Close();

2. Get a document library object and add the file to them.

SPFolder myLibrary = myWeb.Folders["DocumentLibraryName"];

SPFile myFile = myLibrary.Files.Add(myLibrary.ServerRelativeUrl + "/" + FileUpload1.FileName, bytes, false);

3. Update document library.

myLibrary.Update();

Now if you have to add file meta data to the upload file proceed as following.

1.  Get the SPListItem relative to the uploaded file and save information.

SPListItem Item = myFile.Item;

Item["ColumnName"] = "my value";

2. Update the item.

Item.Update();

 

The entire code will look like:

using Microsoft.SharePoint;

using System;

using System.IO;

using System.Web.UI.WebControls;

 

namespace CDG

{

    class Exemple

    {

        protected FileUpload FileUpload1;

 

        public void foo_method()

        {

            using (SPSite mySite = new SPSite("http://server/sites/site"))

            {

                using (SPWeb myWeb = mySite.OpenWeb())

                {

                    Stream myStream = FileUpload1.PostedFile.InputStream;

                    byte[] bytes = new byte[Convert.ToInt32

                                                     (FileUpload1.PostedFile.ContentLength)];

                    myStream.Read(bytes, 0, bytes.Length);

                    myStream.Close();

 

                    SPFolder myLibrary = myWeb.Folders["DocumentLibraryName"];

                    SPFile myFile = myLibrary.Files.Add(myLibrary.ServerRelativeUrl + "/" +

                                                      FileUpload1.FileName, bytes, false);

                    myLibrary.Update();

 

                    SPListItem Item = myFile.Item;

                    Item["ColumnName"] = "my value";

                    Item.Update();

                }

            }

        }

    }

}

 

Cheers.

Find Memory Leak in Application in SharePoint

I invite you to read one of my colleague's article. This article explains how to find your SharePoint Memory Leak. Just follow the link http://dirkvandenberghe.com/archive/2008/09/25/find-a-memory-leak-application-on-sharepoint.aspx and enjoy your read.

How to Backup and Restore SSP (Shared Service Provider)

I post this article in response of one of my colleague's problem . Suppose that you delete by accident the dbs of the SSP (Shared Service Provider) and you have to restore it afterwards.

Back up Your SSP

Before restoring it, the first thing to know is how to backup it. :)

Back up using the user interface

  1. Go to the operations page in the Central Administration, in the Backup and Restore Section, Click Perform a backup.
  2. Select the SSP you want to backup. Click to Continue to Backup Options.
  3. On the Backup Options page:
    • Verify if your SSP is present.
    • The type of backup needs to be Full.
    • Select the UNC path of the backup folder.
  4. Click OK.

If you go to the status page you will see the status of your backup. It refreshes every 30 seconds.

Back up using the SSP using the command line

Execute this command :

stsadm -o backup -directory SERVER\FOLDER_NAME -backupmethod full -item SSP_NAME

If you have a warning or an error you will find information in your log file spbackup.log in the folder you specified.

Back up the SSP using the SQL Server

  1. Go to the SQL Server Management Studio and connect to your database server.
  2. Expand Databases in the Object Explorer.
  3. Right click on the SSP database, go to Tasks, then click Back Up.
  4. Select the full Backup Type.
  5. Select the Database option.
  6. Type the name and the description of the backup.
  7. Set when it will expire.
  8. Specify the location to store the backup.
  9. Click OK.

 

Restore Your SSP

Now that you know how to back up your SSP, here the way to restore it.

Restore the SSP using the user interface

  1. Go to the operations page in the Central Administration, in the Backup and Restore Section, click Restore from Backup.
  2. Enter the UNC path to the backup folder in the Backup File Location section.
  3. Select the backup file that you want to restore, click Continue Restore Process.
  4. Select the SSP you want to restore, Click Continue Restore Process.
  5. On the Restore Option page :
    • Verify if you select the good SSP.
    • Select Same configuration in the Restore Options and accept the warning message.
    • Verify the names and URLs.
  6. Click OK.

Restore the SSP using the command line

  1. Catch the GUID of the backup you want to restore in executing this command :
    • stsadm -o backuphistory -directory UNC_PATH_TO_BACKUP_FOLDER
  2. Now get the name of the SSP you want to restore using this command :
    • stsadm –o restore –showtree -directory UNC_PATH_TO_BACKUP_FOLDER -backupid GUID
  3. And now restore the SSP (the PATH_FROM_TREE is the full farm path as showed by the -showtree parameter)  :
    • stsadm -o restore -directory UNC_PATH_TO_BACKUP_FOLDER -backupid GUID -item PATH_FROM_TREE -restoremethod overwrite
  4. Type y and press ENTER.
  5. Type the user name and the password.

If you have a warning or an error you will find information in your log file sprestore.log in the backup shared folder.

Restore using the SQL Server

  1. Stop WSS Timer service and don't restart it until you restore the database. Make sure all running procedures have completed before you continue.
  2. Go to the SQL Server Management Studio and connect to your database server.
  3. Expand Databases in the Object Explorer.
  4. Right click on the SSP database, go to Tasks, then to Restore, click to Database.
  5. Specify the destination and the source and select the backup you want to restore.
  6. Click Options in the Select a page section.
  7. Click OK.
  8. Restart the WSS Timer service.

 

For more information regarding Back up and Restore I invite you to consult those articles on TechNet as I did for writing this article :

Back up SSPs (Office SharePoint Server 2007)

Restore SSPs (Office SharePoint Server 2007)

Back up databases

Restore Databases

Comment copier les champs qui composent un élément vers un autre élément ?

Voici comment simplement copier les champs d'un élément vers un autre élément.

Allez chercher ouvrez l'élément source et l'élément de destination. Par exemple :

SPSite Site = new SPSite("http://server/");
SPWeb Website = Site.OpenWeb();
SPList SourceList = (SPList) Website.Lists["Source"];
SPList DestinationList = (SPList) Website.Lists["Destination"];

Créez l'élément de destination

SPListItem DestinationItem = DestinationList.Items.Add();

Voici maintenant comment copier les divers types de champs. Dans l'Object Modeling de SharePoint chaque type de colonne est présenté par un objet. Je reprends ici uniquement les principaux.

Pour le texte
SPFieldText

Pour le texte multiple
SPFieldMultiLineText

Pour le choix
SPFieldChoice

Pour les nombres
SPFieldNumber

Pour les valeurs monétaires
SPFieldCurrency

Pour les dates et les heures
SPFieldDateTime

Pour les utilsateurs et le groupes
SPFieldUser

Il faut tout d'abord aller chercher la valeur que vous voulez copier en procédant comme ceci, par exemple :

SPFieldText SingleLine = SourceList.Items[0].Fields["Source Item Title"] as SPFieldText;

Maintenant que la valeur est récupérée. Faites la copie proprement dite :

DestinationItem["Destination Item Title"] = SingleLine.GetFieldValueAsText(SourceList.Items[0]["Source Item Title"]);

Puis faire un update de l'élément.

DestinationItem.Update();

Vous pouvez maintenant facilement appliquer cette méthode à tous les types de champs. :)

How to copy an item's field to another item's field ?

Here a simple way to copy fields of an item to another item

Open the source and the destination item. For exemple :

SPSite Site = new SPSite("http://server/");
SPWeb Website = Site.OpenWeb();
SPList SourceList = (SPList) Website.Lists["Source"];
SPList DestinationList = (SPList) Website.Lists["Destination"];

Create the destination item

SPListItem DestinationItem = DestinationList.Items.Add();

In the SharePoint Object Modeling every column type is represented by a object type. Here a list of the most used column type :

Single line
SPFieldText

Multiple line
SPFieldMultiLineText

Choice
SPFieldChoice

Number
SPFieldNumber

Currency
SPFieldCurrency

Date and Time
SPFieldDateTime

Users or Group
SPFieldUser

Now, get the value of the source item, for exemple like that.

SPFieldText SingleLine = SourceList.Items[0].Fields["Source Item Title"] as SPFieldText;

Make the copy of the field.

DestinationItem["Destination Item Title"] = SingleLine.GetFieldValueAsText(SourceList.Items[0]["Source Item Title"]);

After that just make an update.

DestinationItem.Update();

As you see, it's really easy to make a copy from an item's field to another item's field. :)

1 - 5 Next
Subscribe to my feed !

 ‭(Hidden)‬ Admin Links