Archive

Archive for the ‘MOSS’ Category

Set Content Types of default list and libraries

July 6th, 2011 Comments off

We discussed the branding of the MySite through feature stapling in previous post. Now, what if you have to change the content types of the personal and shared document libraries in the personal MySites.

Unfortunately you cannot do it from feature stapling. As your feature stapler executed before the creation of document libraries and it cannot find the document libraries to set the content type. In simple words you need to set content type after site provisioning, so your code can see the document libraries. I thought we can have a control on the master page which should run on the first time user open the site. But need to run just once.

So the solution is to use the power of SharePoint delegate controls. I created a custom control and then loaded that control in a SharePoint delegate control through a feature. This control set the content types for libraries and after completing the job it deactivated the feature. This deactivation actually removes the custom user control from SharePoint Delegate control. So it executed just once and I got the desired results from an elegant solution.

Lets start the work now……

Use the same project we created last time for MySite branding. Create a custom user control in the MySiteBranding project. Your project will look like this at this stage.

Now add a new feature to hook up your custom user control in a delegate control. Add an empty element to the project and add it to the new ContentTypeFeature. Remember to set the feature scope to Site.

Now load your custom user control to the AdditionalPageHead delegate control. Change your elements.xml file as below.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Control Id="AdditionalPageHead"
           Sequence="80"
           ControlSrc="~/_controltemplates/MysiteBranding/ContentTypeHook.ascx"></Control>
</Elements>

Here is the code for custom user control. I’m assuming that you already added the content types to the site. You can add it through feature stapler.

/// <summary>
/// Defines the MySiteHook class.
/// </summary>
public partial class MySiteHook : UserControl
{
    /// <summary>
    /// Page load method.
    /// </summary>
    /// <param name="sender">object for sender</param>
    /// <param name="e">object for events args </param>
    protected void Page_Load(object sender, EventArgs e)
    {
        SPWeb web = SPContext.Current.Web;
        SPSite site = web.Site;
        string contentTypeFeatureGuid = "6369c736-eff8-4c72-9397-b56fbc456a3b";
        string personalDocLibName = "Personal Documents";
        string sharedDocLibName = "Shared Documents";
        try
        {
            web.AllowUnsafeUpdates = true;

            // Get the SPList objects
            SPList sharedList = web.Lists[sharedDocLibName];
            SPList personalList = web.Lists[personalDocLibName];

            // Your content types has to be added to the site.
            SPContentType customContentType = web.ContentTypes["Salman Docs"];
            AddAsDefaultContentType(sharedList, customContentType);
            AddAsDefaultContentType(personalList, customContentType);
            web.Update();

            // Deactivate the feature that this control is linked to so that this code will not run again.
            bool hasFeature = false;
            foreach (SPFeature feature in site.Features)
            {
                if (feature.DefinitionId.ToString().Equals(contentTypeFeatureGuid, StringComparison.CurrentCulture))
                {
                    hasFeature = true;
                    break;
                }
            }

            if (hasFeature)
            {
                site.Features.Remove(new Guid(contentTypeFeatureGuid));
            }
        }
        finally
        {
            web.AllowUnsafeUpdates = false;
        }
    }

    /// <summary>
    /// This method add the content type as default content type.
    /// </summary>
    /// <param name="list">The SPList object.</param>
    /// <param name="customContentType">The SPContentType object.</param>
    private static void AddAsDefaultContentType(SPList list, SPContentType customContentType)
    {
        // Enable content type management on this list
        list.ContentTypesEnabled = true;
        list.Update();

        // Add the custom content type
        list.ContentTypes.Add(customContentType);
        list.Update();

        // Set it as default content type
        foreach (SPContentType contentType in list.ContentTypes)
        {
            if (contentType.Name != customContentType.Name)
            {
                list.ContentTypes.Delete(contentType.Id);
                list.Update();
            }
        }
    }
}

You have no need to make any change in the master page. If you look in the default master page, you can see that AdditionalPageHead delegate control exists there.

<SharePoint:DelegateControl runat=”server” ControlId=”AdditionalPageHead” AllowMultipleControls=”true”/>

Ok so the last thing you have to do is to add your feature to site definition. Use the existing feature stapling concept and just add this feature as well in the elements file for MySiteStapler. Your feature file will look like this now.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <!-- Feature to staple with MySite host site collection -->
  <FeatureSiteTemplateAssociation Id="13719e8d-0a3d-4a1a-97a9-3e6aa9adf4c5" TemplateName="SPSMSITEHOST#0" ></FeatureSiteTemplateAssociation>
  <!-- Feature to staple with MySite personal site collection -->
  <FeatureSiteTemplateAssociation Id="13719e8d-0a3d-4a1a-97a9-3e6aa9adf4c5" TemplateName="SPSPERS#0" ></FeatureSiteTemplateAssociation>
  <!--MySiteDocs: Feature to replace content types in document libraries for MySite personal site collection-->
  <FeatureSiteTemplateAssociation Id="6369c736-eff8-4c72-9397-b56fbc456a3b" TemplateName="SPSPERS#0" ></FeatureSiteTemplateAssociation>
</Elements>

Ok all is done now just create the site with new site definition. So when you create your new MySite, it will change the master page as we discuss in the last post. Also it will load your custom control to the AdditionalPageHead delegate control. When user opens the MySite first time your control’s code will change the content type of the shared and personal document libraries. So all is done. Your project finally looks like this.

You can use this approach for anything which needs to be done after site provisioning.

Hide the ‘Pages’ library in SharePoint breadcrumb

May 11th, 2011 Comments off

SharePoint breadcrumb is a nice feature to have on the pages. But it doesn’t fit very easily in usual scenario. If you also want to get rid of ‘Pages’ library link in the breadcrumb of a publishing site then see the below instructions.

Normal breadcrumb

My Portal -> Pages -> Test Page1

Updated breadcrumb

My Portal -> Test Page1

Then change the breadcrump source provide to CurrentNavSiteMapProviderNoEncode.  You have to change this in SiteMapPath tag like this…..

<asp:SiteMapPath SiteMapProvider=”CurrentNavSiteMapProviderNoEncode” id=”ContentMap” SkipLinkText=”" NodeStyle-CssClass=“ms-sitemapdirectional runat=“server”/>

 Now when we use CurrentNavSiteMapProviderNoEncode, we have to make sure that ‘Show Pages’ is true for current navigation. Even if you don’t want to display pages on the left navigation you have to enable it. Then you can hide your pages one by one from the navigation settings.

Cheers

Unique Column/Field in SharePoint List

August 9th, 2010 6 comments

It is always a desire to have unique field in SharePoint list. Sometimes you have to replicate a database table and sometimes it could be a special requirement. Recently when I have been asked to have a unique field in a SharePoint list, I searched for it. Thanks to CodePlex. There is a solution called Unique Column Policy.

It is actually a feature. It creates a new information policy that allows you to specify a column in a list or library that should have unique values. Unfortunately on the website there is not enough information on how to use it. So I created this blog, I will show how it works.

First of all you need to download it. You can download it from this link. (Download). Then you need to install it on your SharePoint server. For this addsolution to solution store.

stsadm –o addsolution –filename UniqueColumnPolicy.wsp

Now deploy the solution. You can deploy it either from the stsadm command or you can deploy it using the SharePoint Central Administration site. This solution deploys globally. So you cannot just deploy it for a single site collection. Once your solution is deployed. Now you can create unique columns in SharePoint lists.

In order to, apply it on a column. Create a new column in a SharePoint list. Now open the list settings page and open the Information management Policy Settings link.

List Settings Page

In order to, apply it on a column. Create a new column in a SharePoint list. Now open the list settings page and open the Information management Policy Settings link. Select ‘Define a Policy’ and click ‘OK’.

Information Management Policy Settings

It will open edit policy page. Now select ‘Enable Unique Column Policy’.

Edit PolicySelect the field you want to use for unique values.

Select a field

Now when you try to add the same value you used before in unique column. It will give you an error like this.

Unique Column ErrorIt will show the same error when you try to update and item and give it same value you used before in any other list item. Good thing is this also works with datasheet view.

DataSheet View

Like in the above image, we tried to use the same value UC_999 in unique column field. When we moved to next column. It displayed an error icon in front of the affected row. This is the error to notify user that duplicated values are not allowed in this column. Click on the icon and you can see more details.

DataSheet View - Error Details

In simple words this feature is amazing, and it gives more power to SharePoint list.

I hope this will help implementing unique column/field in SharePoint list.

MOSS – Change Maximum File Upload Limit

June 17th, 2010 Comments off

By default you can upload a maximum file of 50 MB in a SharePoint document library. It might be very rare when you want to increase this limit but sometime it happens. Recently it happened with me.
It’s not really difficult.
Open SharePoint Central Admin site
Click on Application Management
Click on Web Application General Settings

Now on this screen you can changes the maximum file upload size.

You may also need to change the timeout settings for your webpage to upload big files.
 

You may also need to change the timeout settings for your webpage to upload big files.

Have fun

Create Workflow Using SharePoint Designer 2007

June 13th, 2010 1 comment

It is part of the same series of workflows creation I started a few days ago. As promised this time here is the example to create a copy item workflow using SharePoint designer 2007 for SharePoint 2007. Obviously there is no much difference between the workflows creation process of SharePoint designer 2010 and SharePoint designer 2007.

This is an example for copy item workflow. Whenever you create or change an item in a list it will copy your item in the destination list.
Open the SharePoint designer and open your website. Select new from file menu and then Workflows to create a new workflow.

 

Give your workflow some name and select the list/library to initiate workflow. Also select when the workflow triggers. I selected on item created and item changed.

 

In this workflow we will put a condition. Actually I created a field in document library. This is a ‘Yes/No’ field. This workflow will check for the field value and triggers only if it is ‘Yes’.

 

Select the condition in workflow’s settings page and select the first option to compare list field for condition.

 

Select field name to MoveFlag from the drop down list.

Set the value to ‘Yes’ for condition. It defines that if MoveFlag value will be equal to Yes then this workflow will trigger.

Now it’s time to set the action statement. Your condition is done, now select Action. Select ‘Copy List Item’ from the drop down list.

 

Select the source list name in first instance and select destination list name in second instance. I selected the ‘Issued Documents’ library for source list. This statement will look like this.

 

All done, now just click finish it will process your workflow and associate it with the source list as well.

 

Now what else, start using the workflow. It’s ready and will work as required. In next post I will explore the programmatic way to create workflows. That would be more interesting I think.

Thanks

 Thanks

Check if User Has Permissions on List or WebSite

June 11th, 2010 3 comments

Recently I was working on a web part where I have to check for current user permissions for different lists in multiple sub-sites. I found a very straight forward method from SharePoint API. (DoesUserHavePermissions)

This method has some variations on site and list levels. 

On site level this method can take two parameters, string loginname and SPBasePermissions. You can use these parameters with two variations. If you don’t pass the login name then it verifies the permission level against the current user. SPBasePermissions is an enumeration. You can find more details about SPBasePermissions here.

On list level this method can again take two parameters, but three variations in method parameters. If you just pass the SPBasePermissions then it verifies the permission level against the current user. Then you can check permissions for a specific user and in third variation you can apply folder check. You can find more details on SPList.DoesUserHavePermissions here

You can use this method like this

if (oWeb.DoesUserHavePermissions(SPBasePermissions.ViewPages))
{
       SPList oList = oWeb.Lists[ListName];

        if (oList.DoesUserHavePermissions(SPBasePermissions.ViewListItems))
        {
         // some smart code
        }
}

Thanks

Web Part Management/Maintenance Page

June 11th, 2010 Comments off

Sometimes very small things make life difficult for you. Like any of your web part can mess up your page and you cannot view your page. You left with no other option to open the page in an editor like SharePoint Designer and then remove the web part to make your page working again.
There is a very simple solution without opening your page in SharePoint Designer, just put the ?contents=1 in the end of your page’s URL. It will open a Web Part Maintenance page. On this page you can easily close or remove any web part.

I hope you like the tip of the day.

SharePoint Central Administration – Service Unavailable HTTP Error 503

May 28th, 2010 18 comments

Today when I after installing the active directory on my SharePoint box, I found that all of a sudden my Central Administration site stopped working. And keep on giving error message

Error: Service Unavailable HTTP Error 503. The service is unavailable.

In IIS console I noticed that App Pool for the central administration site was stopped. I restarted it, but again when I tried to open the site. App pool stopped again. And this happen again and agin. After spending some time. I realized that because I installed active directory, I need to refresh crendtails attached to App pool.

To change the identity acount attached to app pool, select app pool and right click on it. Select the ‘Advanced Settings’.

One thing to notice is that previously if you defined the user like servername\administrator, then it won’t work now. If you use this way it will give message that username doesn’t exist. Because now you have set up the Active Directory.

 Just use administrator and password and it will work or use domain/username and password.

I hope it will help.

Thanks

Use Custom CSS in MOSS RadEditor

November 17th, 2009 Comments off

Telerik’s MOSS RadEditor is a great editor, especially if you are concerned about accessible html outputs. If you are using MOSS RadEditor, you might notice that it doesn’t pick your custom styles at edit time. Reason is by default it uses this style file “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\wpresources\RadEditorSharePoint\4.5.6.0__1f131a624888eeed\RadControls\Editor\Skins\Default2006\EditorContentArea.css”. You have two options one you can edit this file and add your style here.

Second option is that you can set the css file by using the property “CssFiles” in ConfigFile.xml or ListConfigFile.xml depends on your configuration. For example you are deploying your CSS files in below folder structure under layouts folder.

“LAYOUTS/MyWebFiles/css/editor_style.css”

your entry in config files will be like this.

<property name=CssFiles><item>/_LAYOUTS/MyWebFiles/css/editor_style.css</item></property>

Cheers

Categories: MOSS Tags: , , ,

SharePoint Warmup Scripts – Scheduled

November 2nd, 2009 Comments off

It’s very common that when you start any SharePoint site early in the morning, you find it very slow. I know a lot of organizations are very annoyed by this behavior. Normally IIS discard it’s cache overnight, so in the morning first request rebuild the web application again. This make first request very slow. This is an amazing script, it will seek out every site and site collection on a sharepoint server, going as deep as you like, and send them an HTTP request, so that the site remains in cache.

I don’t take credit for this script, I picked this from http://code.msdn.microsoft.com/spwakeup (Thanks to A Kennel), and this project was inspired from SharePoint warmup scripts at : http://blogs.msdn.com/joelo/archive/2006/08/13/697044.aspx .

For this script you need to consider a few things, first of all STSADM should be in path. I used Windows 2008, so here you can see how to add STSADM in your path using Windows 2008. Attached SPWarmup.zip file contains 4 files. startup.cmd, spwakeup.exe, sites.conf and Program.cs.

The exe (spwakeup.exe) from A Kennel recognizes 3 option switches:
-depth:X –specifies how many levels deep to search for sites. If you do not set anything, the default value of 3 is used.
-site:XXX.XXX — specifies the starting URL. If you do not set anything, the app looks for sites.conf, and uses the sites listed there. If sites.conf does not exist, the default URL http://localhost is used.
-log:Filename — specifies a log file to record the results. If a file is not specified, the log will be displayed on the console.

This is the example that how yout startup.cmd will look like

“c:\SPWarmupServer\SPWakeUp” -depth:5 -log:c:\SPWarmupServer\logs.txt -site:http://www.mysite.com

Now here you can see how you can schedule a script in windows server 2008. By using this post you can schedule your script to a suitable time for daily call. It could be 4:00 am. It will give site a boosting effect.

SPWarmupScript.zip

Cheers.

Categories: MOSS Tags: , ,