Apr 2, 2010
How to access the Sharepoint control from Silverlight module through javascript
How to access the silverlight method through javascript
- In the Loaded event or the constructor of the page we have to make call to the ( RegisterScriptableObject) method. It accepts two parameters:
- A name, which is used to make calls to the application.
- An object, which will be called.
Oct 5, 2009
Function to write the cookies in the browser for infinite time in javascript
Using SharePoint:SPSecurityTrimmedControl to control the visibility of SiteActions
“<SharePoint:SiteActions>”
Sep 29, 2009
How to access the Content Place Holder and Control placed inside some place holder in master page programmatically
There is requirement to access the control placed inside the PlaceHolderMain in the master page.
Enjoy!!!
Sep 25, 2009
How to attach a process when activating or deactivating features from command prompt.
This is bit tricky one as often we have some feature receiver class with the feature and we want to debug it .
When we activate the feature through UI (ie from the site features) we attach the w3p process and debug the feature receiver code but this does not happen while doing the same from command prompt
So for this follow these steps :
1) Type the command to activate the feature in command prompt :
Stsadm –o activatefeature –name “feature name” –url
2) As soon u press enter , press Pause button from the keyboard.
There should not be any delay in these two actions as this is very fast process .
3) Now go to the solution in visual studio and attach the “stsadm” process from attach process tab
4) Now come to command prompt and press any key to resume the process.
Now you can debug the feature receiver code
So your job is done.
Enjoy!!!
Sep 19, 2009
Add custom groups in the quicklaunch of the site
Here is the code sample..
site.AllProperties["vti_associategroups"] += ";" + oGroup.ID;
add custom group in the quicklaunch collection in the following way..
happy coding..
How to get the SP User ‘Owner” object of the site
SPUser owner =null;
owner = subSite.SiteGroups.GetByID(int.Parse(subSite.Properties["vti_associateownergroup"]));
Similarly Members group and visitor group can be accessed
SPUser member =null;
owner = subSite.SiteGroups.GetByID(int.Parse(subSite.Properties["vti_associatemembersgroup"]));
SPUser visitor =null;
owner = subSite.SiteGroups.GetByID(int.Parse(subSite.Properties["vti_associatevisitorgroup"]));
Apply custom master page on the application pages(Branding of Sharepoint Site)
Normally in a sharepoint site ,we find that look and feel of the site is not consistent through out the site, there may be application pages which are different from the home page of the site.Now the problem is that since these are admin pages , so we cannot do much modification on these pages, and moreover if we go in sharepoint designer and changes the page from there than these changes will appear at farm level. So there is another way branding these admin pages is through HTTP Module, which will change the master page of a particular admin page at run time without effecting any thing on the page. This way we can have consistent look and feel through out the site.
Steps :
1) Open visual studio
2) Create a new project of type class library
3) Add sharepoint reference to it
4) Inherit the class from “IHttpModule” class
Follow this code:
namespace MyModule
{
public class LogoutModule : IHttpModule //inherit from the class
{
public void Init(HttpApplication app)
{
// Create a event handler to execute the process
app.PreRequestHandlerExecute += new EventHandler(app_PreRequestHandlerExecute);
}
void app_PreRequestHandlerExecute(object sender, EventArgs e)
{
HttpContext context = HttpContext.Current; // Get the current context of the pagee
Page page = context.CurrentHandler as Page;
if(page!=null)
{
page.PreInit +=new EventHandler(page_PreInit);
}
}
// Application master page can be changed only on the PreInit method of the page only
void page_PreInit(object sender, EventArgs e)
{
Page page = sender as Page;
if (page != null)
{
if (page.MasterPageFile.Contains("application.master"))
{
page.MasterPageFile = @"\_LAYOUTS\CustomApplicationMaster\Customapplication.master";
}
}
}
public void Dispose()
{
}
}
}
Your job is done..
Enjoy!!!
Redirect the page using HTTP module
Steps :
Steps :
1) Open visual studio
2) Create a new project of type class library
3) Add sharepoint reference to it
4) Inherit the class from “IHttpModule” class
Follow this code:
---------------------------------------------
namespace MyModule
{
public class LogoutModule : IHttpModule //inherit from the class
{
public void Init(HttpApplication context)
{
// Create a event handler to execute the process
context.EndRequest += new EventHandler(context_EndRequest);
}
void context_EndRequest(object sender, EventArgs e)
{
Page page = HttpContext.Current.CurrentHandler as Page;
if (HttpContext.Current.Request.Url.Equals("http://spvm:1212/Pages/Default.aspx"))
{
HttpContext.Current.Response.Redirect("http://spvm:1212/sites/dms/Pages/Default.aspx");
}
}
}
}
----------------------------------------------------
To execute the HttpModule it should be registered in the Web.Config file of site and dll in the GAC.
This is the entry in the web.config file
Under <httpModules>tag Section
----------------------------------------------------
<add type="ClassName,AssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4da00116c38ec5" />
-------------------------
Your job is done..
Enjoy!!!
Injecting java script in sharepoint page programmatically using Http Module
Objective here is to create a Modal Disclaimer when user tries to edit his details in his MySite.
For this on the detials page a modal disclaimer will appear before users start editing/modifying their profile information they must acknowledge a “disclaimer” telling them about the ramifications of what it is they are doing. This disclaimer should be a modal dialog, i.e. they must read it and click it away.
Constraint :
Cannot touch the OOB EditProfile Page i.e cannot write any java script in the OOB page.
Solution : We have to embed javascript in the editprofile page..each time when the page loads and it should be displayed when the page loads and then modal pop window should come on top of the page…
For this we wrote one HTTP module which checks the URL of the current requested page and then takes the reference of Content Place holder main and then add javascript inside that place holder as control..
Since this modal pop window should fire when the loading of the page completes i.e javascript should fire with some differ..so for this we used ScriptLink link control :
Code for HTTP module
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
}
void context_PreRequestHandlerExecute(object sender, EventArgs e)
{
HttpContext currentContext = HttpContext.Current;
Page page = currentContext.CurrentHandler as Page;
if (page != null)
{
page.LoadComplete += new EventHandler(page_LoadComplete);
}
}
Code snippet for adding the control to the page
void page_LoadComplete(object sender, EventArgs e)
{
Page page = sender as Page;
if (page != null)
{
//Checking if the current requested page is the EditProfile page of the My Site then only proceed.
if (HttpContext.Current.Request.Url.AbsolutePath.ToString().Trim().ToUpper(System.Globalization.CultureInfo.InvariantCulture).Contains("/_LAYOUTS/EDITPROFILE.ASPX"))
{
//Get the PlaceHolderMain content place holder from the application master page
//to add the Script link control in the page
Control c = page.FindControl<ContentPlaceHolder>("PlaceHolderMain");
if (c != null)
{
ScriptLink link = new ScriptLink();
link.Defer = true;
link.Localizable = false;
//This is the .js file to open the ModalDialog box when user goes to EditProfile page
//this is the java script file which contains the code for the modal pop window
link.Name = "MyFolder/MySiteModalWindow.js";
c.Controls.Add(link);
}
else
{
//Since control object is null so don't insert ScriptLink control in the page
}
}
}
else
{
//Do not inject any script in the page
}
}
This way we can embed java script in to the page through Http module without touching the OOB page.
Enjoy!!!
Using linq query to get the data from the list or any collection
In this example we are querying the web feature collection by using this query
SPFeatureCollection webFeatureColl = web.Features;
var webActivatedFeatureresults = from SPFeature feature in webFeatureColl
where feature.Definition.DisplayName == featureToActivate
select feature;
List<SPFeature> webActivatedFeature = webActivatedFeatureresults.ToList<SPFeature>();
Same way we can query the list collection.this is faster than query the list collection using the CAML query.
More on Linq will be next post.If you have any important stuff to share regarding the same,then keep posting
Enjoy coding !!!
How to get all the web of which the current logged in user is part of(Added in any of the Groups)
Sample Code:
SPWeb oWeb = SPContext.Current.Web;
SPWebCollection oWebColl = oWeb.GetSubwebsForCurrentUser();
//This gives all the web object for which the current logged in user is
part of.
Enjoy !!!
Creating a long running operation page
Code for this:
SPLongOperation operation = new SPLongOperation(this) {
LeadingHTML = "This is the description of my operation",
};
operation.Begin();
Thread.Sleep(2000);
operation.End("/Custompage.aspx");
Note: Put your code inside Operation.Begin() and Operation.End()
For more information visit
Microsoft.SharePoint.SPLongOperation
Enjoy!!!
Creating custom groups and permissions associated with the groups
So first we are going to create the custom group
---------------------------------------
---------------------------------------------------------------
Once the group is created we will create the custom permission level..
---------------------------------------------------------------
---------------------------------------------------------------
Once the custom permission level is create ,now its time to bind this permission level to custom created group
----------------------------------------------------------------
---------------------------------------------------------------------
So your custom group with custom permission level is ready to use..
Enjoy!!!
How to programmatically get the Profile of the user from the MySite profile page
So to get the profile of the user (current logged or visited user) programmatically ,we use "ProfilePropertyLoader sharepoint object .This works only on profile of the user and not on the home page.
This is the code below to load the profile
-------------------------------------------
ProfilePropertyLoader loader = ProfilePropertyLoader.FindLoader(this.Page);
if (loader != null)
{
Microsoft.Office.Server.UserProfiles.UserProfile profile = loader.ProfileLoaded;
if (profile != null)
{
String AccName = Convert.ToString(profile["AccountName"].Value, CultureInfo.InvariantCulture);
}
}
-----------------------------------------
Thus u can get the all the properties from the profile loader
Enjoy!!!
How to remove the value from the OOB choice type field in the column in list
Here is the sample code for this..
-------------------------------------------------------------------
SPFieldChoice fieldName = (SPFieldChoice)ListName.Fields["ColumnName"];
//Check if the required choice value exist in the field
//If it exists then remove it from the choice collection
if (fieldName.Choices.Contains("ChoiceValue") == true)
{
fieldName.Choices.Remove(ChoiceValue);
fieldName.Update();
}
else
{
// Do nothing.Choice value does not exist
}
-------------------------------------------------
This way we can access any type of the column type provided by Sharepoint and add or remove the values programmatically
Your Job is done!!!
Enjoy!!!
Accessing SPUser from SPFiledUser
Similary we can access any column created using “People and Group” column type
Here is the sample code .Here I am taking the 1st object from the task List.
-----------------------------------------------------------
SPSite site = new SPSite("http://spvm:2409");
SPWeb oSubWeb = site.OpenWeb();
string assignedTo = string.Empty;
//Get the first item from the “Task” List
SPListItem oItem = oSubWeb.Lists["Tasks"].Items[0];
if (oItem["Assigned To"] != null)
{
assignedTo = oItem["Assigned To"].ToString();
SPFieldUserValue spfl = new SPFieldUserValue(oSubWeb, assignedTo);
SPUser oUser = spfl.User;
if (oUser != null)
{
assignedTo = oUser.Email;
//userName = oUser.Name;
}
}
----------------------------------------------------------
So your job is done.
Enjoy!!!
Hide the template from the site templates ( on create site page) through java script.
Now the problem was the required template was available under the custom template section ,so on page load first template section comes as default selected,so i have to select the appropriate template to check the available templates.
I tried to hide the template through object model ,but through object model I can remove the template permanently from the site,but here i have to hide the template.
So I tried this objective using javascript on the /_layouts/create.aspx page,which is not although not recommended to modify the OOB pages,To insert the javascript in to the page through HTTPmodule without touching the OOB page follow this post Link ,
but again challenge was this "Input Template Picker" control is a user control and on page loads it displays the template in the first tab only,so i checked the page view of the page and it contains only the value in the values in the first tab,
on clicking the next tab,control fires event through javascript and populates the control,so I was not able to capture the event.
Then while surfing I came across one blog which mentions about capturing the event when there is any change in page object,Bingo...now when i selects another tab , I was able to capture the event and write my logic to hide the template.
Here the javascript code contains an "onDocumentChange" DOM function which gets called when something changes on the page,Inside there is another delegate function which contains the logic is to get the collection of all the available option tag and then make the outerHTML value to empty.
Note: Here is another catch,first i tried to make the optionTag[i].Text to empty but what happened in the template selection panel,value disappeared but it still occupies the space and highlights on selection,so I make the outerHTML of the optionTag to empty.
So your job is done!!!
If you have any other method to do the same throug object model or through javascript then do reply
Enjoy!!!
How to get the Query string value through javascript
Requirement is to get the querystring value through javascript.
Here Querystring variable name is “Tag”
Add this function in the javascript block of the page and call according to the requirement.I am calling this function on page load.
Here is code:
-----------------------------
function querySt() {
var url = window.location.search.substring(1);
var qSColl = url.split("&");
for (i=0;i<qSColl.length;i++) {
qsValue = qSColl[i].split("=");
if (qsValue[0] == 'Tag') {
var tabValue = qsValue[1];
}
}
}
---------------------------------
So this way you can access any variable value from the querystring
So your job is done
Enjoy!!!