Tuesday, September 23, 2014

Site Closure and Delete policy in SharePoint 2013

Introduction

We have an option to set a policy to close and delete for SharePoint sites. Let we see what are all the steps involves for the same.

Site Policy

Site policy is like a definition which covers all the protocols of closure and deletion. We need to set this in Site Collection. Policies will available in sub sites once it defined in top level site. Steps to set Site Policy,

  • Goto Site Settings in Top Level Site
  • Click Site Policy in Site Collection Administrations

  • Choose Create to set up a policy
  • Fill up the options of new site policy 

  • Here, I given example to delete a site automatically. The Site will be deleted after 4 days of site creation. We will get an Email notification 1 day before and it is having an option to follow up by Email. We may postpone the deletion for a specific period if site admin wants to extend. Postpone period also set up here. Once you done with option click Ok. Policy has created and list out in Site Policies.

Site Closure and Deletion

After policy set up is done. We need to inherit this policy in sub sites. Then only our site will be deleted automatically as per policy. Here is the steps to apply the policy

  • Goto Site Settings in your subsite
  • Click Site Closure and Deletion in Site Administration
  • Here, The policies will populate in Site Policy Dropdown. We have to choose the policy from Dropdown and Click Ok. Once you applied the policy, It will show up, when you site is going to delete and Enable the postpone option if you set in Policy. 

That's all Site Closure and Deletion has applied for your site. Your site will be Closed/Deleted as per Site Policy.

Sunday, September 21, 2014

Set Owner for a Group in SharePoint site programmatically

Introduction

As all we know, We have an option to keep group with set of people in SharePoint. This group is having some permission to access the site. Generally, We have three groups in a site like Owner, Contributor and Visitor. Owners having rights to do all in site. Contributor is allowing to access the site with limited permission. Visitor is having rights to just visit a site.
We may set an owner to these groups. By default, It will take Site Collection admin as an Owner of the group. Let us see how to modify that programmatically.

Customization

var groupName = SPContext.Current.Web.Groups["YourGroupName"];
groupName.Owner = "UserName"; //Give as SPUser
groupName.Update();
SPContext.Current.Web.Update();

That's all group owner has set to the group.

Wednesday, September 17, 2014

Referring CSS in Application Page

We may get confused to refer a css in application page. That is, Where it should be placed. Because, It will show an error if we placed the css wrong place.

Here the correct place to refer

<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
    <link href="Your css URL" rel="Stylesheet" id="linkStyle" runat="server" type="text/css" />

</asp:Content>

Tuesday, September 16, 2014

Set Target Audience for a webpart programmatically

Introduction

As all we now SharePoint webpart properties having an option to set Target Audience. If we set this to particular member/group, the webpart will show to them. Let we see how to set this by programmatically

Customization

At First, we need to create a site page and add a webpart in it programmatically using visual studio. Refer this URL to add an webpart in sitepages. Then add an Event receiver to Feature. call  the below method in Feature Activating method

 private void SetTargetAudienceToAdmin(SPFeatureReceiverProperties properties, string relativeUrlOfPage)
        {
            
            try
            {
                var web = properties.Feature.Parent as SPWeb;
                
                    using (web)
                    {
                        web.AllowUnsafeUpdates = true;
                        SPFile page = web.GetFile(web.Url + "/" + relativeUrlOfPage);
                        if (page.Exists)
                        {
                            SPServiceContext serviceContext = SPServiceContext.GetContext(web.Site);
                            AudienceManager audienceManager = new AudienceManager(serviceContext);
                            SPLimitedWebPartManager wpManager = web.GetLimitedWebPartManager(page.Url, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
                            foreach (System.Web.UI.WebControls.WebParts.WebPart wp in wpManager.WebParts)
                            {
                                if (wp.Title.ToLower().Equals("YourWebpartTitle"))
                                {

                                        wp.AuthorizationFilter = string.Format("{0};;;;", Your GroupName/UserName);
                                        wpManager.SaveChanges(wp);
                                   
                                }

                            }
                        }
                        web.Update();
                        web.AllowUnsafeUpdates = false;
                    }

            }
            catch (Exception ex)
            { throw ex; }
        }

Tha's all. Audience has set to Your webpart.


Wednesday, September 3, 2014

Delete subsites within site collection using Powershell

Here i am going to give a script to delete all the webs within site collection. The script,

Get-SPSite  Your site collection URL| Get-SPWeb -Limit All | ForEach-Object {Remove-SPWeb -Identity $_ -Confirm:$false}

This script will access the rootweb also but it can't be deleted. To avoid this, try the below

$url = " Your site collection URL"
$subsites = ((Get-SPWeb $url).Site).allwebs | ?{$_.url -like $url +"/*"}


foreach($subsite in $subsites) { Remove-SPWeb $subsite.url }