Posts tagged sharepoint

Sharepoint 2010 – How to launch the new/upload file dialog from Javascript

2

If you need to brand Sharepoint to the point of hiding the ribbon, you might want to still have some functions available via custom buttons in your interface. For example, if you have a document library/list and want to create a new folder or upload a document, you would need to have a custom button in your interface to do so.

The problem is how to actually launch the Sharepoint modal dialog to use these functions from Javascript. Fortunalty, Microsoft provides the Sharepoint Object Model for Javascript which exposes most of Sharepoint functions to the client side (as long as the logged in user has access obviously).

Important: In the code below, the ctx.listUrlDir variable is used to determine the relative path to the NewForm.aspx file. The variable is actually declared automatically as long as you have the list/document library webpart displayed on the page. Otherwise, you need to replace that variable with the relative path to the NewForm.aspx file.

List – Create New Item

SP.UI.ModalDialog.showModalDialog({
	url: ctx.listUrlDir + "/NewForm.aspx?IsDlg=1",
	title: "Create New Item"
	dialogReturnValueCallback: function() {
		window.location = window.location.href;
	}	        
});

Document Library – Upload New Document

var exppath = getQSParameterByName('RootFolder');
SP.UI.ModalDialog.showModalDialog({
	url: ctx.listUrlDir + "/Forms/Upload.aspx?IsDlg=1" + ((exppath != null && exppath.length > 2) ? '&RootFolder=' + exppath : ''),
	title: "Upload a document",
	dialogReturnValueCallback: function() {
		window.location = window.location.href;				
	}	        
});

The getQSParameterByName function code is provided at the end of this post.

Document Library – Create New Item of Custom Type

var exppath = getQSParameterByName('RootFolder');
SP.UI.ModalDialog.showModalDialog({
	url: ctx.listUrlDir + "/Forms/Upload.aspx?ContentTypeId=0x012000bd2db3db2b004dda815269e70974b696&IsDlg=1" + ((exppath != null && exppath.length > 2) ? '&RootFolder=' + exppath : ''),
	title: "Create New Custom Type Document",
	dialogReturnValueCallback: function() {
		window.location = window.location.href;				
	}	        
 });

You must replace the value of the ContentTypeId parameter with your own!

The getQSParameterByName function code is provided at the end of this post.

Open in Windows Explorer (IE only)

CoreInvoke('NavigateHttpFolder', exppath, '_blank');

In most cases where a Sharepoint modal is involved, you can replicate any built-in function of the ribbon by looking at the URL called (via Firebug) and adapting the scripts above.

The getQSParameterByName function:

function getQSParameterByName(name) {
    var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}

Sharepoint 2010 – Tableless webparts guide

8

If you tried to create even simple page layouts, you probably noticed the mess Sharepoint creates with tables when placing webpartzones and webparts in the page. While searching about a way to remove completely these tables, I found an interesting solution by David Schneider, which removes tables completely but creates a new whole set of issues with Sharepoint. While the tables are removed around webparts and webpartzones, it breaks functionality of the ribbon in lists and some ajax functionnality on default Sharepoint controls. Not really convenient.

Fortunately, with the help of a colleague, we found a way to remove the tables only on defined webpartzones. Zones marked with a property called tableless=”true” will be stripped from their tables while all other Sharepoint controls won’t be affected at all.

So what needs to be done in order to get this functionnality? Follow these steps:

1) Copy the following modified C# code (notice the tableless property that was added to David Schneider’s solution) in a file called AKS_WebPartZone_Adapter.cs and place it in the AppCode directory under your server root directory (like C:\Inetpub\wwwroot or C:\Inetpub\wwwroot\wss\VirtualDirectories\80).

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;

/// WebPartZone Adapter
/// Created by David Schneider
/// http://blog.sharepoint.ch
/// Based on AKS

namespace AKSAdapters
{
  public class AKS_WebPartZone_Adapter : System.Web.UI.Adapters.ControlAdapter
  {
      protected override void Render(HtmlTextWriter writer)
      {
          bool inEditMode = false;
          System.Web.UI.WebControls.WebParts.WebPartZone wpz = Control as System.Web.UI.WebControls.WebParts.WebPartZone;
          if (wpz != null && wpz.Attributes["tableless"] != null)
          {
              SPWebPartManager swpm = (SPWebPartManager)SPWebPartManager.GetCurrentWebPartManager(wpz.Page);
              inEditMode = !swpm.GetDisplayMode().AllowPageDesign;
          }
          if (inEditMode)
          {
              // Render the WebPartZone
              writer.Indent++;
              writer.AddAttribute(HtmlTextWriterAttribute.Id, wpz.ID);
              if (!String.IsNullOrEmpty(wpz.CssClass))
              {
                  writer.AddAttribute(HtmlTextWriterAttribute.Class, wpz.CssClass);
              }
              if (wpz.LayoutOrientation == System.Web.UI.WebControls.Orientation.Horizontal)
              {
                  writer.AddAttribute(HtmlTextWriterAttribute.Class, "AspNet-WebPartZone-Horizontal");
              }
              else if (wpz.LayoutOrientation == System.Web.UI.WebControls.Orientation.Vertical)
              {
                  writer.AddAttribute(HtmlTextWriterAttribute.Class, "AspNet-WebPartZone-Vertical");
              }
              writer.RenderBeginTag(HtmlTextWriterTag.Div);
              writer.Indent++;

              // Render the web parts
              if (wpz.WebParts.Count > 0)
              {
                  WebPartCollection wpColl = new WebPartCollection(wpz.WebParts);

                  foreach (System.Web.UI.WebControls.WebParts.WebPart wp in wpColl)
                  {
                      writer.WriteLine();
                      writer.AddAttribute(HtmlTextWriterAttribute.Class, "AspNet-WebPart");
                      writer.RenderBeginTag(HtmlTextWriterTag.Div);

                      wp.RenderControl(writer);
                      writer.RenderEndTag(); // Div
                      writer.Indent++;
                  }
              }
              writer.RenderEndTag(); // Div

              writer.Indent--;
              writer.WriteLine();
          }
          else
          {
              // If we are editing the page --> render the web part as usual.
              base.Render(writer);
          }
      }
  }
}

2) Edit the compat.browser file which in the AppBrowser directory located in your server directory and add the following piece of code after the opening tag:





3) In your page layouts, add the property tableless=”true” to all your WebPartZones.

Your pages will then be free of tables without affecting core Sharepoint functionality such as the ribbon and other editing functions. Enjoy.

Sharepoint – Masterpage User Controls Limit

0

If you insert more than 10 user controls in a masterpage, you might get an error similar to this:

The page ‘/path/somefile.master’ allows a limit of 11 direct dependencies, and that limit has been exceeded.

You can easily increase that limit as it is actually set in your sharepoint web.config:

1) Browse to C:\inetpub\wwwroot\wss\VirtualDirectories\

2) Open the folder corresponding to your Sharepoint website.

3) Edit the file web.config

4) Do a search for “safemode“.

5) Change the property DirectFileDependencies from 10 to a value your choice.

6) Do an iisreset command and you’re done!

Sharepoint – The missing configuration screen

1

This Sharepoint Configuration Wizard screen was removed from the final Sharepoint build, at the very last minute:

Sharepoint Level of Difficulty

Sharepoint 2010 – Display compilation errors

3

By default, Sharepoint displays a friendly error box that tell pretty much nothing about the error that just occurred. It is possible to display the default yellow screen of death with all the details, found in usual ASP applications.

1) Navigate to C:\inetpub\wwwroot\wss\VirtualDirectories\

2) Open the Sharepoint web application folder you wish to edit.

3) Edit the web.config file.

4) Find the property customErrors and set it’s mode value to Off.

5) Find the property compilation and set it’s debug value to true.

6) Find the property SafeMode and set it’s CallStack value to true.

7) Do a iisreset.

Done!

Sharepoint 2010 – Custom Error Pages

4

Unless you love the default Sharepoint error pages for 404 (not found) and 401 (access denied), it is usually a good idea to change them. The steps are a bit different than a standard ASP.net application.

1) Browse to C:\inetpub\custerr\

2) You should now see a list of localization folders like en-US or fr-FR. Open the folder you want to edit and modify the error pages for 401,403,404 and 500.

3) Now browse to C:\inetpub\wwwroot\wss\VirtualDirectories\ and open the folder associated to the Sharepoint site you would like to affect.

4) Edit the file web.config and find the keyword customErrors. Replace the complete customErrors code block with:

<customErrors defaultRedirect="404.htm" mode="On">
	   <error redirect="401.htm" statusCode="401" />
	   <error redirect="403.htm" statusCode="403" />
	   <error redirect="404.htm" statusCode="404" />
	   <error redirect="500.htm" statusCode="500" />
</customErrors>

5) Now find the keyword <system.webServer>. Inside this block, place the following block code. (If the httpErrors block already exists, replace it completely!)

<httpErrors errorMode="Custom" existingResponse="Auto">
</httpErrors>

6) Save web.config and do the usual iisreset command if needed.

Sharepoint – Access QueryString from custom webpart

2

I was trying to access the QueryString parameters inside a custom webpart coded in C#. I would usually use Request.QueryString but the Request object was simply not available in this case. Turns out it is found under the Page object:

string kquery = Page.Request.QueryString["kqry"];

Sharepoint Designer – “The server could not complete your request” error message

3

I started getting this error message yesterday while working in Sharepoint Designer. I would get the message whenever I would open a Sharepoint website. The solution to this problem is quite simple:

1) Open Internet Information Services (IIS) Manager.

2) Expand your computer list on the left then click on Application Pools.

3) Locate the application pool used by your Sharepoint website.

4) Right-click on the application pool and select Recycle.

You should now be able to connect to your Sharepoint website using Sharepoint Designer as usual.

Go to Top