Monthly Archives: January 2014

Restrict Sharepoint to don’t upload office files in Picture/Video Lib

i have implemented a solution where client asked to don’t upload office files and pdf’s in a Picture Library or video library. so for that i call ItemAdding event for that list where we need to restrict for uploading and did following code.

public class ListEventRevievers : SPItemEventReceiver
{
public override void ItemAdding(SPItemEventProperties properties)
{
if (properties.List.Title == "ListName")
{
string beforeUrl = properties.BeforeUrl;
string extension = beforeUrl.Substring(beforeUrl.LastIndexOf('.'));
Match match = Regex.Match(extension, @"^.*\.(docx|xlsx|DOCX|XLSX|doc|DOC|pdf|PDF|ppt|pptx)$", RegexOptions.IgnoreCase);

if (match.Success)
{
properties.Status = SPEventReceiverStatus.CancelWithError;
properties.ErrorMessage = “You are not allowed to upload excel,word, power Point and pdf files.“;
}
}
}
}

if user try to upload any restricted file then a popup will appear like this
error

Writing EventReceivers SharePoint Lists

i am very pleased to share one of the best solution ever i have in my mind which we have implemented in every SharePoint project, that is often we need to do some work around when we add/update/delete any item from SharePoint list. These are known as List Event Receiver in SharePoint, i use this to quickly remember it is something like writing Triggers as we write in SQL Server on a Table.

Work around
Need to create 2 Classes one for ListFeatureReceiver.cs (to Assosiating EventRecievers With List(s)) this would help to add events like itemadding, itemAdded and item Updated etc on activation of specific feature, and second one ListEventReceiver.cs (containts events along with their action items)

at the end i’ll send email alerts on ItemAdded

Note: To create any library/List from code write that code in ListFeatureReceiver.cs

ListFeatureReceiver.cs Code

using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public class ListsFeatureReceiver : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
base.FeatureActivated(properties);

SPWeb web;
SPList aList;

if (properties.Feature.Parent is SPWeb)
{
web = (SPWeb)properties.Feature.Parent;
}
else
{
web = ((SPSite)properties.Feature.Parent).RootWeb;
}

if (web != null)
{
aList = web.Lists.TryGetList("JobApplication");
if (aList != null)
{
AssosiatingEventRecieversWithList(aList,SPEventReceiverType.ItemAdded,
Assembly.GetExecutingAssembly().FullName,
"Qatalum.Inet.Sharepoint.ListEventReceiver");
}
}
}

private void AssosiatingEventRecieversWithList(SPList _list, SPEventReceiverType _type, string _assembly, string _class)
{
_list.ParentWeb.AllowUnsafeUpdates = true;
_list.EventReceivers.Add(_type, _assembly, _class);
_list.Update();
_list.ParentWeb.AllowUnsafeUpdates = false;
}
}

To Attach ListFeatureReceiver.cs to feature
add a new feature and click on it, go to its properties write Receiver Assembly and Receiver Class (complete path of ListEventReceiver.cs class with namespace)

ListEventReceiver.cs Code

public class ListEventRevievers : SPItemEventReceiver
{
public override void ItemAdded(SPItemEventProperties properties)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
if (properties.ListItem.ParentList.Title == "JobApplication")
{
SendJobApplicationStatusAlerts(properties);
}
});
}

private void SendJobApplicationStatusAlerts(SPItemEventProperties properties)
{
try
{
string ToEmail = string.Empty;
SPWeb web;
web = (SPWeb)properties.Web;
string Subject = properties.ListItem["FieldName"].ToString();
string body = properties.ListItem["FieldName"].ToString();
string FromEmail = sproperties.ListItem["FieldName"].ToString();
string ToEmail = properties.ListItem["FieldName"].ToString();
string SMTP = "YourSMTP address";

bool isSent = SendEmail(SMTP, FromEmail, ToEmail, Subject, body);
}
catch (Exception ex)
{
throw ex;
}
}

private bool SendEmail(string smtpServer, string _sFrom, string _sTo, string _sSubject, string _sBody)
{
bool _bResult = false;
try
{
string smtpFrom = string.Empty;
if (string.IsNullOrEmpty(_sFrom))
{
smtpFrom = SPAdministrationWebApplication.Local.OutboundMailSenderAddress;
}
else
{
smtpFrom = _sFrom;
}

//Create the mail message and supply it with from and to info
MailMessage mailMessage = new MailMessage(smtpFrom, _sTo);

//Set the subject and body of the message
mailMessage.Subject = _sSubject;
mailMessage.Body = _sBody;
mailMessage.IsBodyHtml = true;

SmtpClient smtpClient = new SmtpClient(smtpServer);
smtpClient.Send(mailMessage);
_bResult = true;
}
catch (Exception ex)
{ ex.ToString());
_bResult = false;
throw ex;
}
return _bResult;
}
}

SharePoint Site Backup and Restore Commands

Power shell commands

For BACKUP:

Backup-SPSite -Identity webUrl -Path C:\Backup\ProjectName.bak

For Restore:
Restore-SPSite -Identity webUrl -Path C:\Backup\ProjectName.bak -Force -Confirm:0

=================== Using STS ADM Commands =========
Backup command:
stsadm -o backup -url http://moss:4002/ -filename c:\bkupsite.bak
To restore:
stsadm -o restore -url http://moss:4004/ -filename c:\bkupsite.bak

Open sharepoint list Newform in popup and refresh

i am very pleased to share one more solution i.e how to open sharepoint list NewForm.aspx in Popup and then refresh the parent, here is the code


function OpenNewFormAsPopUp() {
var options = {
title: "Create New Tour Request",
width: 600,
height: 775,
url: "/Communication/Lists/TourRequest/NewForm.aspx",
dialogReturnValueCallback: silentCallback
}
SP.UI.ModalDialog.showModalDialog(options);
}
function silentCallback(dialogResult, returnValue) {
location.reload(true);;
}