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 }

Wednesday, August 27, 2014

Site Pages library is missing in SharePoint site

We may face "SitePages list doesn't exist" issue while activating feature. To resolve this problem, We need to activate "Wiki Page Home Page" feature.


Tuesday, August 26, 2014

Showing Image Popup while clicking images in NewsFeed Webpart

Introduction

I already given some ideas about Newsfeed webpart. Now, I am going to give an idea to show image popup when we click an image in NewsFeed.


Customization

Here too we can do the customization newsfeed webpart in both Direct and Programmatic way. 

Direct

After placing Newsfeed webpart in sitepage, Then, Upload a jquery library in Site Assets. Download the plugins here Add a Script Editor webpart in same page. Edit the Script Editor and paste below script.

<script type="text/javascript" src="/SiteAssets/jquery.min.js"></script>
<script type="text/javascript" src="/SiteAssets/jquery.popupoverlay.js"></script>

<script type="text/javascript">

$('.ms-microfeed-attachmentImage').addClass('feedPopup_open'); //adding class to image
 $('#feedPopup').popup(); 
//On click method for an image

 $('.ms-microfeed-attachmentImage').click(function () {

        var image_href = $(this).attr("src"); //getting image source
        $('#divFeedDescPopup').html('<img src="' + image_href + '" />'); //appending an image with popupblock
    });

</script>


<!-- Style for popup -->

<style>
.popupContent p label {
    display: block;
    font-weight: bold;

}
.popupContent img {
    width:100%;

}
</style>

<!-- Popup block -->
<div id="feedPopup" class="popupBlock" style="width: 30%; display: none">
<div id="divFeedDescPopup" class="popupContent"> </div>
 <button class="feedPopup_close popupClose">Close</button>
</div>


That's all save the page. Popup will display for each images in newsfeed webpart.

Programmatically

Add a sitepage in your solution within module. And paste the jquery library files in _layout folder. Then, Refer those library files in sitepage. Paste the above script, style and popup block in the page. Deploy the solution and see the popup while clicking an image in Newsfeed.


Setting Row Limit in CAML Query

Here, I am going to give an example to set RowLimit for list items in CAML Query.

Query

<View>
<Query>
<OrderBy>
                                    <FieldRef Name="Created" Ascending='FALSE'/>
</OrderBy>
</Query>
<ViewFields>
<FieldRef Name="ID"/>
<FieldRef Name="Title"/>
<FieldRef Name="Job_x0020_Title"/>
<FieldRef Name="Email_x0020_ID"/>
<FieldRef Name="Member_x0020_Name"/>
</ViewFields>
<RowLimit Paged="TRUE">1</RowLimit>

</View>