Recent Blogs

AvatarByRashid Aziz

Creating users alerts using PowerShell in SharePoint 2010

We had a requirement in SharePoint 2010 to automate the creation of alerts based on SPS group membership. After spending some time searching for solution I have managed to write this code in PowerShell to automate the process. I think it worth sharing with you guys.

Creating users alerts using PowerShell code:

$web = Get-SPWeb "http://eblogin.com"
$group = $web.Groups["eblogin members"]
$list = $web.Lists["eblogin documents"]
foreach ($user in $group.Users){
     $alert = $user.Alerts.Add()
     $alert.Title = "eblogin alerts"
     $alert.AlertType = [Microsoft.SharePoint.SPAlertType]::List
     $alert.List = $list
     $alert.DeliveryChannels = [Microsoft.SharePoint.SPAlertDeliveryChannels]::Email
     $alert.EventType = [Microsoft.SharePoint.SPEventType]::Add
     $alert.AlertFrequency = [Microsoft.SharePoint.SPAlertFrequency]::Immediate
     $alert.Update()
}
$web.Dispose()

You can also change the DeliveryChannels option to SMS  and AlertFrequenct option to Daily. You could run different scripts for your different SharePoint groups.

AvatarByRashid Aziz

Moving Items Between SharePoint Lists

I came across a scenario where I want to copy large amount of content from one sharepoint list to another.  I have done this by creating custom view on lists and copied contents using datasheet view.

Please follow these steps to achieve this.

  • Create one list view for both lists which should contain all the columns that you want to copy, also make sure that columns are not read only and they are not system generated columns like “Created by, Created”.
  • Open both lists in different browsers
  • Select newly created view for both list
  • Open this view in datasheet view on both lists
  • Select all the items in the source list and click copy
  • Select the first cell of the available row in the destination list and click paste
  • All your items should be copied successfully, now you can open the list in standard view and see that all data is copied.

This works as long as you don’t need to copy any of the system generated content like (created by filed). You need to use list view web parts or write custom code to achieve that.

AvatarByRashid Aziz

Copy mysite across SharePoint farms

In SharePoint when you want to transfer mysite content from one farm to another. You can do this via database backup/restore of mysite content database. I recently came across a scenario while I was working with a client where I need to migrate few employee mysites but not all of them. They have about 200 employees from whom they want to transfer 5 employees my sites to the new environment.

Here is the way to migrate user’s mysites to new farm one by one

Go to your farm where mysite is hosted

Open the command prompt and navigate to the following path C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN

Run stsadm command to back up the site

Stsadm.exe –o backup –url http://my/sites/<oldalias>-filename e:\newalias.dat

Copy the site back up to the destination farm and restore it with new alias

Stsadm.exe –o restore –url http://my/sites/<newalias>-filename e:\newalias.dat

Migrate old account to new one

Stsadm.exe –o migrateuser –oldlogin domain\oldalias –newlogin domain\newalias –ignoresidhistory

You need to update existing user profile, update the personal site attribute and the PersonalSIteURL attribute if exist.

Now the migration of one user mysite is done. Inform the user with the new link to have access to his/her my site, The new URL will look like this http://my/sites/<newalias>. 

AvatarByRashid Aziz

Show files extension in document library view

If you have a custom view on document library where you need to show the each file name with extension. This is fairly simple and can be done this way.

  • Open your document library view in SharePoint designer and follow these steps
  • Open your web site the SharePoint designer
  • Go to you document library and open the yourviewname.aspx file in design view.
  • Select the WebPartPages:ListViewWebPart control
  • Right click on WebPartPages:ListViewWebPart control and select “Convert to XSLT data View”
  • Right click on the column where you want to show file extension and select “Insert formula”
  • In the right hand box delete the existing entry and insert @FileLeafRef
  • Click “Save” on the SharePoint designer ribbon.
  • Refresh the document library and now you will see the document name with extension in the new column.

AvatarByRashid Aziz

Remove “View all site content” link in SharePoint 2007

Follow these steps to remove “View All Site Content” link form SharePoint 2007 site

1. Open your site master page in SharePoint designer

2. Go to the code and you will see the following

<Sharepoint:SPSecurityTrimmedControl runat="server" PermissionsString="ViewFormPages">
<div>
<SharePoint:SPLinkButton id="idNavLinkViewAll" runat="server" NavigateUrl="~site/_layouts/viewlsts.aspx" Text="<%$Resources:wss,quiklnch_allcontent%>" AccessKey="<%$Resources:wss,quiklnch_allcontent_AK%>"/>
</div>
</SharePoint:SPSecurityTrimmedControl>

3. You need to change the PermisisonString attribute value from “ViewFormPages” to “BrowseDirectories”

4. Now save the master page changes.

5. Go to the site, if you are authenticated user only then you will see the “view all site content” link,   it will be not visible for anonymous users.

Note: The “BrowseDirectory” permissions are same for members and owner permission levels for SharePoint 2007. But it is not set for the “Limited access” permission levels.

AvatarByRashid Aziz

Custom master page deployment in SharePoint 2010

Today I am going to show you that how easily you can deploy your custom master page as a SharePoint feature and it automatically change the site default master page when you activate this feature.

For this you need to create new empty SharePoint project.

Add a new item to the project and select module and call is “Module1”.

Once the module is created it contains “element.xml” and “sample.txt” file, rename the “sample.txt” file to “customMaster.master” as shown in below image.

             

Now you can copy code from your custom master page which you want to deploy(If you don’t have your custom code then you can use SharePoint 2010 designer and copy the contents of v4.master and past them in your customMaster.master file in Visual Studio. Make the changes you want to.

Copy the following code and replace your “element.xml” file.

<?xmlversion="1.0" encoding="utf-8"?>
<Elementsxmlns="http://schemas.microsoft.com/sharepoint/">
  <ModuleName="Module1" Url="_catalogs/masterpage">
    <FilePath="Module1\customMaster.master" Url="customMaster.master" />
  </Module>
</Elements>

Now you have to add feature event receiver, so on feature activation this custom master page will be your default master page.

To do this, right click on your feature1 and select “Add event receiver”

Replace the “FeatureActivated” and “featureDeactivating” method with the followings

public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite site = (SPSite)properties.Feature.Parent;
            SPWeb web = site.RootWeb;
            Uri masterUri = new Uri(web.Url + "/_catalogs/masterpage/custommaster.master");
            web.MasterUrl = masterUri.AbsolutePath;
            web.CustomMasterUrl = masterUri.AbsolutePath;
            web.Update();
        }
 public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite site = (SPSite)properties.Feature.Parent;
            SPWeb web = site.RootWeb;
            Uri masterUri = new Uri(web.Url + "/_catalogs/masterpage/v4.master");
            web.MasterUrl = masterUri.AbsolutePath;
            web.CustomMasterUrl = masterUri.AbsolutePath;
            web.Update();
        }

Now deploy the feature, when you will activate the feature it will replace you default master page with “customMaster.master” , and when you deactivatethe feature it will replace  to default master page which is V4.master.

AvatarByRashid Aziz

SharePoint feature stapling with site definition

I have created a site definition, there are many ways to use features with this site definition,

You can create a site from this site definition and deploy feature on that newly created site, you can also include features in onet.xml of site definition file and then create site from it. Another way to do is by feature stapling which associate your feature to the site definition.

In this blog I will demonstrate how you can use feature stapling to associate your features with site definition.

To use the  stapling, you need features which you like to map with the site definition.

You need to create a second feature which does the stapling.  The solution sample is given below

The manifest.xml file if you are wrapping your feature in a solution. 

<?xmlversion="1.0"encoding="utf-8"?> 
<Solutionxmlns="http://schemas.microsoft.com/sharepoint/"SolutionId="0579AAC2-9E7E-478E-AFE9-D5EC426055E4">
  <FeatureManifests>
    <FeatureManifestLocation="EB.MyDemo.FeatureStapling\feature.xml" />
  </FeatureManifests>
</Solution>

Note: The feature stapling will not affect any sites which have been already created from a site definition, only subsequent site will take the affect. But you can activate features individually on the site.

AvatarByRashid Aziz

Create sub site programmatically in SharePoint 2010

Sometime we require creating a sub site programmatically in code. Here is the simple way you can create a sub site programmatically.

(SPSite site = new SPSite(http://siteCollectionUrl))

  { using (SPWeb web = site.OpenWeb())

        {try
            { web.AllowUnsafeUpdates = true;
               //Create new web site
               SPWeb newWeb = site.AllWebs.Add("Site url", " Site name", "Site description", 1033, {Name of the Site Template}, true, false); 
               newWeb.Update();
            }
            catch (Exception)  { }
            finally { web.AllowUnsafeUpdates = false; } 
             } }

 

Name of the site templates

E.g. Use “STS#0” for Team site or “BLOG#0” for Blog site.

Available site templates

Site Template Names

 

Team Site

 

STS#0

Blank Site

STS#1

Document Workspace

STS#2

Blog

BLOG#0

Group Work Site

SGS#0

Visio Process Repository

VISPRUS#0

Basic Meeting Workspace

MPS#0

Blank Meeting Workspace

MPS#1

Decision Meeting Workspace

MPS#2

Social Meeting Workspace

MPS#3

Multipage Meeting Workspace

MPS#4

Assets Web Database

ACCSRV#1

Charitable Contributions Web Database

ACCSRV#3

Contacts Web Database

ACCSRV#41

Issues Web Database

ACCSRV#6

Projects Web Database

ACCSRV#5

Document Center

BDR#0

Records Center

OFFILE#1

Business Intelligence Center

BICenterSite#0

My Site Host

SPSMSITEHOST#0

Personalization Site

SPSMSITE#0

Enterprise Search Center

SRCHCEN#0

Basic Search Center

SRCHCENTERLITE#0

FAST Search Center

SRCHCENTERFAST#0

Enterprise Wiki

ENTERWIKI#0

Publishing Portal

BLANKINTERNETCONTAINER#0

Publishing Site

CMSPUBLISHING#0

Publishing Site With Workflow

BLANKINTERNET#2

AvatarByRashid Aziz

Modify web.config settings on feature activation in SharePoint

In SharePoint 2010 there is a way to modify web.config settings file. You need to use SPWebConfigModification class which is a part of Microsoft.SharePoint.Administration namespace. This class allows us to register entities dynamically.

Adding new settings in web.config

I am going to use SPWebModification class inside “Feature Activating” method of SharePoint feature to register a custom assembly.

 public override void FeatureActivated(SPFeatureReceiverProperties properties)

{

SPWebService newService = SPWebService.ContentService;

SPWebConfigModification customModification = new SPWebConfigModification();
customModification.Path = "configuration/SharePoint/SafeControls";
customModification.Name = "SafeControl[@Assembly='CustomAssembly'][@Namespace='CustomNamespace'][@TypeName='*'][@Safe='True']";
customModification.Sequence = 0;
customModification.Owner = "UserName";
customModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
customModification.Value = "<SafeControl Assembly='CustomAssembly' Namespace='CustomNamespace' TypeName='*' Safe='True' />";
newService.WebConfigModifications.Add(customModification);
 
newService.Update();
newService.ApplyWebConfigModifications();

}

Now you need to deploy and activate the feature. After that open web.config of your SharePoint web application where you deployed the feature and will find the following setting being added.J

<SafeControl Assembly="CustomAssembly" Namespace="CustomNamespace" TypeName="*" Safe="True" />
AvatarByRashid Aziz

Embed YouTube video in SharePoint 2010

Some time you need to add YouTube videos in your SharePoint site pages, it is very simple to implement. I’m going to show step by step how to embed YouTube videos in SharePoint 2010 site, using content editor web part.

1.   Go to YouTube video which you want to embed and copy the video embedded code , should be something like this,

<iframe title=”YouTube video player” width=”480″ height=”390″ src=”http://www.youtube.com/embed/CuLxW5sPXhA?rel=0″ frameborder=”0″ allowfullscreen></iframe>

2.   Open notepad and past the above code there, and save the file with name something like “videotest.txt”.

3.   Go to SharePoint site and open a document library and upload the “videotest.txt” file.

4.   Now go to the page where you want to embed the video and add a content editor web part on page.

5.   Go to the document library where you already added “videotest.txt” file, right click on file and click on “copy shortcut”.

6.   Now go back to the page where you have added the content editor web part, click on web part and select “Edit webpart”.

7.   In the “custom link” property paste the “videotest.txt” shortcut value(already copied in step 5)

8.   Click Ok.

 

You can see in the below picture that the YouTube video is embedded in SharePoint page.