•October 21, 2009 • Leave a Comment

I am no longer maintaining this blog for sharepoint related queries.
Please refer http://zoomsharepoint.blogspot.com/ blog for all the updates on sharepoint solutions.

Limiting length of Multiline textbox(inside webpart) through javascript

•September 19, 2009 • Leave a Comment

Here the requirement is to limit the maxlength of multiline text box in webpart .This seems to be very simple problem,just set the property of MaxLength property of TextBox to some no,but when somehow this property does not work with Multiline Text Box..may be there will be some fix in future release.

So  we fix this problem using java script

Add this javascript in the CreateChildControl method.

Here is javascript for this :

———————————————————-

StringBuilder javascript = new StringBuilder();

javascript.AppendLine(“<script type=’text/javascript’>”);

javascript.AppendLine(“function Validation(evt,maxLength)”);

javascript.AppendLine(“{“);

javascript.AppendLine(“if(evt.value.length > maxLength)”);

javascript.AppendLine(“{“);

javascript.AppendLine(“alert(‘Maximum 255 characters allowed’);”);

javascript.AppendLine(“evt.value = evt.value.substring(0,maxLength);”);

javascript.AppendLine(“}”);

javascript.AppendLine(“}”);

javascript.AppendLine(“</script>”);

if (javascript != null)

{

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), “javascriptcode”, javascript.ToString());

}

————————————————————-

In above code we have written the java script and registered the client script in the page.Now we need to add this javascript event in the multiline textbox

————————————————————-txtMultilineTextBox.Attributes.Add(“onKeyDown”, “Validation(this, 255)”);

—————————————————————-

Now we have added the event in the text box for “onKeyDown” event .So whenever user exceed 255 characters in the text box this event will fire and limit the length.

How to get the current logged in user in Event handler

•September 19, 2009 • Leave a Comment

Recently i faced a problem in event handler to do some operation for the current logged in user,but inside event handler SPContext object is null. So after some probe we found a way to get the current user object .

For this we need to get the HttpContext object inside the constructor of the event handler

Here is the sample code :

———————————————–

HttpContext current;

/// <summary>

/// Constructor to get the current logged in user details

/// </summary>

public ListEventHandler()

{

current = HttpContext.Current;

}

————————————————-

Now to get the login name of the current user use this code:

string currentUserLoginName = current.User.Identity.Name;

————————————–

So your job is done

Enjoy!!!

How to get the Query string value through javascript

•September 19, 2009 • Leave a Comment

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’) {

tabValue = qsValue[1];

}

}

}

———————————

So this way you  can access any variable value from the querystring

So your job is done

Enjoy!!!

Accessing SPUser from SPFiledUser

•September 19, 2009 • Leave a Comment

Here requirement is to get the SPUser object from the SPFieldUser. Sharepoint stores the “People and Group” column value in following format id;#domain\user (eg.  1;#SPVM\user) .So we need to get the SPUser object from the SPFieldUser .Once the SPUser object is retrieved ,we can access all the properties of the SPUser like Email,LoginName etc.

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!!!

How to remove the value from the OOB choice type field in the column in list

•September 19, 2009 • Leave a Comment

Here the requirement is to add/remove value from the choice type field from the column created in list programmatically.

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!!!

How to programmatically get the Profile of the user from the MySite profile page

•September 13, 2009 • Leave a Comment

When we visit the MySite,there are two types of pages ,one is default page of the current user and other is the person.aspx which points to the profile of the user .This person.aspx can be of current logged in user or can be of any visited user.
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!!!

Hide the template from the site templates ( on create site page) through java script.

•September 6, 2009 • Leave a Comment

Recently i have interesting requirement,in which i have to hide the specific site templates while creating a subsite.
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.

javascript

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!!!

Creating custom groups and permissions associated with the groups

•September 4, 2009 • Leave a Comment

Here i m going to create the custom groups,custom permission level and association or binding between the group and permission level.

So first we are going to create the custom group

—————————————

SPSite site = new SPSite(“http://spvm:2409″);
SPWeb oSubWeb = site.OpenWeb();
oSubWeb.AllowUnsafeUpdates = true;
oSubWeb.AllowUnsafeUpdates = true;
//Here get the owner group from the site…to get the other groups like member or visitor group follow the earlier post Click here.
SPGroup owner = oSubWeb.SiteGroups.GetByID(int.Parse(oSubWeb.Properties["vti_associateownergroup"]));
oSubWeb.SiteGroups.Add(“Customgroups”, owner, null, “Custom groups”);
oSubWeb.Update();

—————————————————————

Once the group is created we will create the custom permission level..

—————————————————————

SPRoleDefinition role = new SPRoleDefinition();
role.Name = “CustomPermission”;
role.Description = “Custom permission level to View,Edit,Add list items”;
role.BasePermissions = SPBasePermissions.OpenItems
| SPBasePermissions.ViewVersions
| SPBasePermissions.ViewFormPages
| SPBasePermissions.Open
| SPBasePermissions.ViewPages
| SPBasePermissions.UseClientIntegration
| SPBasePermissions.UseRemoteAPIs
| SPBasePermissions.CreateAlerts
| SPBasePermissions.ViewListItems
| SPBasePermissions.ViewPages
| SPBasePermissions.EditListItems
| SPBasePermissions.AddListItems
| SPBasePermissions.DeleteListItems;
oSubWeb.AllowUnsafeUpdates = true;
oSubWeb.RoleDefinitions.Add(role);

—————————————————————

Once the custom permission level is create ,now its time to bind this permission level to custom created group

—————————————————————-

//Name of the custom  group created above
SPGroup oGroup = oSubWeb.SiteGroups["Customgroups"];
oSubWeb.AllowUnsafeUpdates = true;
SPRoleAssignment roleAssignment = new SPRoleAssignment(oGroup);
//Name of the role definition created above            roleAssignment.RoleDefinitionBindings.Add(oSubWeb.RoleDefinitions["CustomPermission"]);
oSubWeb.AllowUnsafeUpdates = true;
oSubWeb.RoleAssignments.Add(roleAssignment);
oSubWeb.AllowUnsafeUpdates = true;</div>

oSubWeb.Update();
oSubWeb.AllowUnsafeUpdates = false;

———————————————————————

So your custom group with custom permission level is ready to use..

Enjoy!!!

Creating Custom Role Definition programmatically

•September 3, 2009 • Leave a Comment

Sharepoint provides many permission level like Full control ,Contributor,visitor etc…but there are scenario where we need to create our own role definitions which may be the combination of permissions available in two different OOB  role definitions..

So through this we can create our custom permissions :

————————————————————————

SPWeb web= SPContext.Current.Web;

SPRoleDefinition role = null;

role.Name = “Custom Role”;

role.Description = “Custom permission level to View,Edit,Add list items”;

role.BasePermissions = SPBasePermissions.OpenItems

| SPBasePermissions.ViewVersions

| SPBasePermissions.ViewFormPages

| SPBasePermissions.Open

| SPBasePermissions.ViewPages

| SPBasePermissions.UseClientIntegration

| SPBasePermissions.UseRemoteAPIs

| SPBasePermissions.CreateAlerts

| SPBasePermissions.ViewListItems

| SPBasePermissions.ViewPages

| SPBasePermissions.EditListItems

| SPBasePermissions.AddListItems

| SPBasePermissions.DeleteListItems;

web.AllowUnsafeUpdates = true;

web.RoleDefinitions.Add(role);

——————————————————

Once this is done ..this permission will be available when we create some group or add some user to the site…

Enjoy!!!

 
Follow

Get every new post delivered to your Inbox.