Upload files to Microsoft Sharepoint Server with Ajax and the ASP .NET FileUpload control

The FileUpload control does not work with AJAX, because it needs a full postback to upload a file. So we have to change the AJAX partial postback to a full postback:

ASPX, add a ScriptManager, UpdatePanel, FileUpload and LinkButton control:

<%@ Page Language="C#" AutoEventWireup="true" Codebehind="Default.aspx.cs" Inherits="WebApplicationCSharp._Default" %>

<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
  Namespace="System.Web.UI" TagPrefix="asp" %>
<%
@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml">
<
head runat="server">
  <title>Web app c#</title>
  <link rel="stylesheet" type="text/css" href="~/CustomStyles.css">
</
head>
<
body>
  <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManagerTest" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanelTest" runat="server">
    <ContentTemplate>
        <asp:FileUpload ID="FileUploadTest" runat="server" />
        <asp:LinkButton ID="LinkButtonTest" runat="server" OnClick="LinkButtonTest_Click">Test Upload</asp:LinkButton>
    </ContentTemplate>
    </asp:UpdatePanel>
  </form>
</
body>
</
html>

Change Page_Load event

protected void Page_Load(object sender, EventArgs e)
{
  // Disable AJAX for the 
LinkButtonTest ONLY. Because we are using the FileUpload control, which does not work with AJAX, we need a full PostBack.
  LinkButtonTest.Attributes.Add("OnClick", String.Format("form1.__EVENTTARGET.value = ‘{0}’; form1.submit(); return false;", LinkButtonTest.UniqueID));
}

Add Click event on LinkButtonTest

///
<summary>
///
The LinkButtonTest is on an AJAX update panel, but by overriding the OnClick event in the Page_Load event,
/// this event will fire with a full postback.
/// </summary>
///
<param name="sender"></param>
///
<param name="e"></param>
protected void LinkButtonTest_Click(object sender, EventArgs e)
{
  this.UploadFile();
}

Add an UploadFile function

public void UploadFile()
{
  using (SPSite sitecollection = new SPSite("http://localhost"))
  {
    using (SPWeb web = sitecollection.RootWeb)
    {
      if (FileUploadTest.HasFile)
      {
        using (Stream fileContent = FileUploadTest.FileContent)
        {
          string fileName = FileUploadTest.FileName;
          string newListItemImageName = ""; 
          // Get list by name
          SPList list = web.Lists[listName];
          // Get rootfolder of the list
          SPFolder folder = list.RootFolder;
          // Delete the current file if it exists. I can’t find a way to check the existence of a file without an exception and not looping through all the items in the list.
          bool fileExists = false;
          SPFile currentFile = null;
          try
          {
            currentFile = folder.Files[fileName];
            fileExists = true;
          }
          catch { fileExists = false; }
          if (fileExists) { currentFile.Delete(); }
          // Add file to the list
          currentFile = folder.Files.Add(String.Format("{0}/{1}", folder.ServerRelativeUrl, fileName), fileContent);
        }
      }
    }
  }
}

Using CustomAction to modify system pages in Microsoft Sharepoint Server

Chris O’Brien’s wrote a great article about, customizing the _layouts/settings.aspx page with CustomActions

http://www.sharepointnutsandbolts.com/2007/07/using-customaction-to-modify-system.html

For example

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomActionGroup
   Id="CustomActionGroupAdminSetting"
   Description=""
   Title="$Resources:Resources, CustomActionGroupTitle"
   Location="Microsoft.SharePoint.SiteSettings"
   Sequence="9" />
  <CustomAction
    GroupId="CustomActionAdminSetting"
    Title="$Resources:Resources,CustomActionTitle"
    Description="$Resources:Resources,CustomActionDescription"
    Location="Microsoft.SharePoint.SiteSettings"
    Sequence="11"

    Id="CustomActionSettings"
    >
    <UrlAction Url="/CustomSettings.aspx"/>
  </CustomAction>
</Elements>

Store Feature, Job or any other administration configuration information in Microsoft Sharepoint Server

There are many way to store configuration information in WSS, but you can use the hierachical object store (SPPersistedObject Class).
This class provides the methods for an object to automatically serialize its state, persist that state in a permanent store, retrieve it at a later time, and deserialize it back into an in-memory object.
It is explained on http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.administration.sppersistedobject.aspx and here http://www.bluedoglimited.com/SharePointThoughts/ViewPost.aspx?ID=271

Stop all MOSS 2007 services

To stop all Microsoft Sharepoint Services 2007 – Windows Services, create a bat file and enter the following lines:

net stop “SPTimerV3”
net stop “SPAdmin”
net stop “SPTrace”
net stop “SQLWriter”
net stop “OSearch”

Developmentimages

Host
VMWare Workstation 6.5.2 (Fit all virtual machine memory into reserved host RAM)

Developmentimage MOSS installation guide
Microsoft Windows Server 2008 x64 sp1
  Use the defaults in the VMWare “Create Virtual Machine wizard”
    – 250GB harddisk
    – 2048MB ram
    – Virtualmachinename: DevMoss200904
    – Customize hardware: Removed floppy
– Windows customalization:
    – Disable the Windows Server 2008 Shutdown Event Tracker
    – Do not show Initial Configuration Tasks at logon
    – Set time zone (GMT+01:00 Amsterdam)
    – Provide computer name and domain (DevMoss200904)
    – Changed windows update to Microsoft update (Use recommended settings)
    – Disable screen saver
    – Change Time (HH:mm:ss) in Control Panel > Customize Regional Options > Time
    – Folder options and filesystem changes:
         – Don’t hide extensions for known file types
         – Don’t use Sharing Wizard
         – Added C:\Projects and C:\Temp
    – Add Web Server (IIS) role
         – All application development
         – All Health and Diagnostics
         – Security Windows Authentication
         – All Management Tools
– Run Microsoft Update
Microsoft SQL Server 2008 x64 Standard (Full installation, SSAS, SSIS, SSRS integrated with sharepoint)
    – Use DOMAIN\Administrator as service account for all services (because this is a development image)
    – Mixed Mode (SQL Server authentication and Windows authentication)
    – Add DOMAIN\Administrator and DOMAIN\Administrators to the SQL Server Administrators role
    – Add DOMAIN\Administrator and DOMAIN\Administrators to the Analysis Services Administrators role
    – Enable FILESTREAM for Transact-SQL access
    – Enable FILESTREAM for file I/O streaming access
    – Allow remote clients to have streaming access to FILESTREAM data
    – Install the Sharepoint integrated mode default configuration
– Run Microsoft Update
Microsoft Office 2007 Enterprise sp1 (Full installation, but skipped publisher,  some tools )
– Run Microsoft Update
Microsoft Visual Studio 2008 x64 with sp1 (Full installation, sp1 enables the dynamic data templates)
– Run Microsoft Update
Microsoft Office Sharepoint Server 2007 x64
     – Advanced installation
     – Complete – Install all componenets. Can add servers to form a sharepoint farm
     – Sharepoint product and technologies configuration wizard:
           – NO, I want to create a new server farm
           – Database server [.]
           – Enter username and password administrator account
           – Specify a portnumber [10000] for the Sharepoint central adminstration web application
           – NTLM (because this is a single server deployment, else we should use kerberos to avoid double hop problem)
Microsoft Office Sharepoint Designer x64 sp1
– Run Microsoft Update
Microsoft Internet Explorer 8
    – Turn off automatic phishing filter
    – Add http://<servername> to the local intranet sites
    – Disabling Internet Explorer Enhanced Security Configuration
    – Changed Google to default home page and default search provider
    – Don’t warn me when closing multiple tabs
    – Open only the first home page when Internet Explorer starts
– Run Microsoft Update
Microsoft Visual Studio extensions for WSS 3.0 v1.3 mar 2009 ctp
Microsoft Enterprise Library 4.1 – October 2008
Mozilla Firefox 3.0.8
LLBLGen Pro 2.60
WinRar 3.80
Redgate .NET Reflector 5.1.4.0
Jetbrains ReSharper 4.1
Notepad++ 5.3.1
– Run Microsoft Update