Wednesday, July 31, 2013

JavaScript to get page and site information like url,.. in SharePoint

use 

alert(_spPageContextInfo.siteAbsoluteUrl);

_spPageContextInfo Object Properties found for SharePoint 2013 application:

  • alertsEnabled: false
  • allowSilverlightPrompt :"True"
  • clientServerTimeDelta :-8980
  • crossDomainPhotosEnabled: false
  • currentCultureName: "en-US"
  • currentLanguage: 1033
  • currentUICultureName: "en-US"
  • layoutsUrl: "_layouts/15"
  • pageListId:  "{df5e2d45-ad0b-4608-a813-ded6a97511db}"
  • pagePersonalizationScope:  1
  • serverRequestPath:  "/sites/dotnetmirror/Lists/Calender custom/calendar.aspx"
  • siteAbsoluteUrl: "http://computerName:5000/sites/dotnetmirror"
  • siteClientTag: "0$$15.0.4433.1506"
  • siteServerRelativeUrl; "/sites/dotnetmirror"
  • systemUserKey: "i:0).w|s-1-5-21-2086049...90378103-3194450012-500"
  • tenantAppVersion: "0"
  • userId: 1
  • webAbsoluteUrl: "http://computerName:5000/sites/dotnetmirror"
  • webLanguage: 1033
  • webLogoUrl:"_layouts/15/images/siteicon.png"
  • webPermMasks: Object { High=2147483647, Low=4294967295}
  • webServerRelativeUrl: "/sites/dotnetmirror"
  • webTemplate: "1"
  • webTitle : "dotnetmirror"

Monday, July 22, 2013

Web part- lists sites based on Site template like blog community

Web part to display all community sites in a site collection, with details,

http://en.share-gate.com/blog/sharepoint-2013-content-search-site-portal

If you took a peek at SharePoint 2013 recently, you may have noticed there is a new Site Collection called the Community Site Portal. This new site works in conjunction with the Community Site template. In short, it’s a site that shows you a list of all Community Sites. My goal is to do the same thing but for other types of Sites, for example a Team Site portal.
SharePoint 2013 Content Search Site Portal
 

Reverse engineering

Best way to understand how SharePoint works is sometimes to dig in to what is presented in front of us. Something quickly jumped at me when I looked at the Community Site Portal, there is a “number of results” shown at the bottom of the page and changes made to the Community Site do not appear immediately. My first thought is that it must be based on the new SharePoint 2013 Search engine. As I mentioned in a previous article, we can leverage the power of search now with the Content Search Web Part or the Search Results to build our sites.
Let’s begin by editing the page and see how this has been built:
SharePoint 2013 Content Search Site Portal


As you can see, the portal is nothing more than the result of a Search Query. It looks like the Content Search Web Part was added with a pre-configured Search Criteria and a custom Display Template. All we need to do now is take a look at the default Query and adapt it to our needs.

The default Content Search Criteria for the Portal

Essentially there is no manual search here; the Web Part is configured with a Search Criteria or a query if you want to display the Community Sites below. We can try to understand it a little better by clicking on the Change Query button in the Web Part properties.
SharePoint 2013 Content Search Site Portal


Then we will have the new SharePoint 2013 Query Builder that is used in many Search Web Parts and settings. Within the Query Builder, we can see right away how it is able to get all the Community Sites for the Portal. And frankly, I am surprised at how easy it is to get it.
SharePoint 2013 Content Search Site Portal
The Query simply says to grab “WebTemplate:Community”; in other words go get all the Sites that are built with the WebTemplate called Community.

Making a SharePoint 2013 Team Site Portal

My first objective is to change the query for the Web Part to something that will get only Team Sites instead of Community Sites. What I did is created a new Site Collection to do my tests based on the Team Site Template. I then Activated the Publishing Infrastructure Site Collection Feature. Finally I created a blank Wiki page in Site Pages and added the Content Search Web Part on it.
SharePoint 2013 Content Search Site Portal


I jumped to the Query Builder of my new Content Search Web Part and doing something I covered recently in “Rolling up SharePoint Sites with Content Search” I erased the Query Text box with whatever query was there by default. Then in the Keyword Filter dropdown menu, I selected the “Return Only Sites” option.
SharePoint 2013 Content Search Site Portal
Why am I selecting this Keyword Filter?
That’s an excellent question, technically I could just modify the “WebTemplate:Community” by one that represents the Team Site WebTemplate.
In this scenario, I am assuming that we do not know what the WebTemplate names are.
By selecting this Keyword Filter and only returning sites, we can jump to the “Refiners” tab in the Query Builder at the top and let SharePoint do the work for us.
SharePoint 2013 Content Search Site Portal
Awesome! Now I know how to write the value for my Query Text. Instead of using “WebTemplate:Community” I can try “WebTemplate:STS” for Team Sites for example.
Let’s go back to the Query Text box and type in our new value:
SharePoint 2013 Content Search Site Portal
Let’s try it out, click on Ok and Save or Apply your changes to see your page again.
SharePoint 2013 Content Search Site Portal


Now you can build your own SharePoint 2013 Site Portal based on any Site Template you want to. You could even push this even further by customizing the Query with other Site Properties. The issue is always in finding what these properties are.
Let’s see if it works with our Blogs as well:
WebTemplate:BLOG
SharePoint 2013 Content Search Site Portal
I am really happy to see how easy it to change the Content Search, even as a Power User I can see this being very helpful to many.


Making it look like the Community Portal – Using the Display Templates

So we managed to build our own SharePoint 2013 Site Portal for Team Sites or even Blogs. But something is missing, the look and feel of the Portal. In my previous post on the Content Search, I already talked about Display Templates and how to change the property mapping to put the values of the properties we want in our favorite Display Template.
Unfortunately, none of the default Content Search Display Templates matches the look and feel we got from the Community Portal. So I am going to cheat and do something I learned with the Content Query Web Part back in SharePoint 2007 and SharePoint 2010. The trick is to export the Web Part found on the Community Portal on to your Desktop and Edit it with Notepad. I noticed that it gets it’s Display Template for items in a System folder of the Site Collection Catalog.
~sitecollection/_catalogs/masterpage/Display Templates/System/Item_CommunityPortal.js
I have a few options; I can export my Content Search Web Part and add the same links for the same properties as the ones in the Community Portal. I can Copy Paste the Display Template from the System folder to Content Web Parts in the Display Templates folder. Obviously I do not recommend the ladder but I do understand that sometimes we just do not have the luxury. You can also go directly to the Display Template stored in the System folder of the Display Templates and change the hidden value from 1 to 0. You will then be able to use it in your Content Search.
SharePoint 2013 Content Search Site Portal

Friday, July 19, 2013

Create site and apply theme programmatically- SharePoint 2013

 Below code will create  a community site with new master page and theme

try
            {
                SPSite site = SPContext.Current.Site;
                             try
                             {
                                 SPWeb newWeb = site.AllWebs.Add(txtTitle.Text, txtTitle.Text, txtDescription.Text, 1033, "COMMUNITY#0", true, false);
                                 newWeb.AllowUnsafeUpdates = true;
                                 newWeb.Update();
                                 newWeb.MasterUrl = "/_catalogs/masterpage/NewMaster.master";
                                 newWeb.AllProperties["__InheritsThemedCssFolderUrl"] = "True";
                                 newWeb.Update();
                                 SPFile file = site.RootWeb.GetFile("/_catalogs/theme/15/Palette012.spcolor");
                                 SPTheme s = SPTheme.Open("S", file);
                                 s.ApplyTo(newWeb, true);
                                 newWeb.AllowUnsafeUpdates = false;
                                 SPUtility.Redirect(site.Url + "/" + txtTitle.Text, SPRedirectFlags.Trusted, Context);
                             }
                             catch (Exception es) { }
                                finally {}
            }

Wednesday, July 3, 2013

Add an item into SharePoint List using Javascript

Below code is used to add list item into the SharePoint list using Javascript,



var FeedbackClientContext;
var FeedbackWebsite;
var FeedbackCurrentUser;
var FeedbackList;
 

function AddFeedbackValue()
{
var FeedbackNoteValue= $.trim($("#txtFeedback").val()); // txtFeedback name of the text box html field
if(FeedbackNoteValue.length > 0)
{
FeedbackClientContext = new SP.ClientContext("/");
   FeedbackWebsite = FeedbackClientContext.get_web();
FeedbackList = FeedbackWebsite.get_lists().getByTitle('Feedback'); // name of the SharePoint list
var FeedbackListItemCreInfo = new SP.ListItemCreationInformation();
   var FeedbackNewItem = FeedbackList.addItem(FeedbackListItemCreInfo);
   FeedbackNewItem.set_item('Feedback',FeedbackNoteValue);
   FeedbackNewItem.update();

   FeedbackClientContext.executeQueryAsync(Function.createDelegate(this, this.onFeedbackQuerySucceeded),
    Function.createDelegate(this, this.onFeedbackQueryFailed));
    }      
}

function onFeedbackQuerySucceeded(sender, args) {
$("#txtFeedback").val('');
}

function onFeedbackQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}


Thanks,
-Sendil

JavaScript to get user details from UserProfileProperties



to get more properties 


Thanks,
-Sendil

Lync Presence in SharePoint - webpart

Sharepoint web part to display the presence(Lync) of an user using Javascript 

is

User Presence 

where as

personProperties.get_email() in

IMNRC(\""+personProperties.get_email()+"\")
is email address of an user which is basically fetched from UserProfile property of current user in Javascript
else if we want to hard code, processed as below

IMNRC("aaa@example.com")

Thanks,
-Sendil