Sunday, September 23, 2012

Create Hyperlink in SharePoint calculate Field

Easy way to create hyperlink in calculate field in sharepoint. Just choose the data type as number.


Thursday, August 16, 2012

Remove Add New Button from sharepoint List Item

Look at the picture below. Do you want to get rid of this button icon when you add it as webpart or view?

It is pretty easy. Just edit its webpart, then in List Views Section, choose No Toolbar.

Set value to SharePoint:PeopleEditor

Set value to SharePoint:PeopleEditor using c# code in visual studio 2010.

Ex.
txbTravelers.CommaSeparatedAccounts = SPContext.Current.Web.CurrentUser.LoginName;

Sharepoint: Get Current Login User

Get Current Login User using c# in Sharepoint

Ex.
var currentUser = SPContext.Current.Web.CurrentUser.LoginName;

Get SPFieldUserValueCollection value from ListItem in SharePoint

This is a way to get user from person/group field in sharepoint.



var item = workflowProperties.Item["Approvers"];
var users = (SPFieldUserValueCollection)workflowProperties.List.Fields["Approvers"].GetFieldValue(item.ToString());

Get value SPFieldUserValueCollection in Sharepoint

Get value from Sharepoint:PeopleEditor and save to list.


Linq expression
            var thisWeb = SPContext.Current.Web;
            var list = thisWeb.Lists["List name"];
            var listItems = list.Items;
            var item = listItems.Add();
            var usersstring = txbTravelers.Accounts;
            var usersfield = new SPFieldUserValueCollection();
            usersfield.AddRange(from object s in usersstring select thisWeb.SiteUsers[s.ToString()]       into user select new SPFieldUserValue(thisWeb, user.ID, user.LoginName));
            item[new Guid("fb51591c-e3d2-401f-9e33-f012c2ff88fa")] = usersfield;
            item.Update();


C# code

var usersstring = txbTravelers.Accounts;
 var usersfield = new SPFieldUserValueCollection();
 foreach (var s in usersstring)
 {
                var user = thisWeb.SiteUsers[s.ToString()];
                usersfield.Add(new SPFieldUserValue(thisWeb,user.ID,user.LoginName));
}

Thursday, July 19, 2012

SharePoint : Show/Hide Columns in New, Edit and Disp Forms

So here is the scenario, you have a issue tracking list and if you only want to introduce certain hidden columns on your New and Disp forms but not your Edit form as an example. You can have any combination for these requirements.

Since the issues are logged by the QA or end-user, we want to hide the Assigned To, Due Date, Priority and Impact from the new form. We want the user editing, typically the dev team lead, to assign these values.

There are several ways you can achieve this.. choose the best that fits your need... or comfortable with.

1. PowerShell
2. Programmatically
3. Custom Forms (SharePoint Designer)
4. ContentTypes
5. jQuery

Using Javascript:

<script language="javascript" type="text/javascript">

_spBodyOnLoadFunctionNames.push("hideFields");

function findacontrol(FieldName) {

   var arr = document.getElementsByTagName("!");
   // get all comments
   for (var i=0;i < arr.length; i++ )
   {
      // now match the field name
      if (arr[i].innerHTML.indexOf(FieldName) > 0)
      {         return arr[i];      }
   }
}

function hideFields() {

   var control = findacontrol("Title");
   control.parentNode.parentNode.style.display="none";
   control = findacontrol("Document Link");
   control.parentNode.parentNode.style.display="none";
   control = findacontrol("PublishDate");
   control.parentNode.parentNode.style.display="none";

}
</script>
Ref: http://sharepointsherpa.com/2008/08/26/sharepoint-2007-hiding-fields-on-newformaspx-and-editformaspx-the-easy-way/ 

Using Powershell : Hide/Show Columns in SharePoint

  • First we need to get the id of the fields to be hidden. A very important tool in your armor is a free tool called SharePoint Manager 2010.
  • Navigate to your Web-App > Site Collection > Site > List > Fields and get the ids of the fields you want to hide
  • Next we will use another important free tool PowerGUI and write the following powershell script




















  • #$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
    #if ($snapin -eq $null) {
    #Write-Host "Loading SharePoint Powershell Snapin"
    #Add-PSSnapin "Microsoft.SharePoint.Powershell"
    #}
     
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint"
    Write-Host "Opening Web" $WebUrl
    $web = Get-SPWeb $WebUrl
     
    ####### Issue Tracker List ######
    $list = $web.Lists["IssueTracker1"];
     
    #Assigned To
    $FieldGuid = New-Object System.Guid("53101f38-dd2e-458c-b245-0c236cc13d1a");
    $Field = $list.Fields[$FieldGuid];
     
    $Field.ShowInNewForm = $false;
    $Field.ShowInDisplayForm = $true;
    $Field.ShowInEditForm = $true;
     
    $Field.Update();
    $list.Update();
     
    #Due Date
    $FieldGuid = New-Object System.Guid("cd21b4c2-6841-4f9e-a23a-738a65f99889");
    $Field = $list.Fields[$FieldGuid];
     
    $Field.ShowInNewForm = $false;
    $Field.ShowInDisplayForm = $true;
    $Field.ShowInEditForm = $true;
     
    $Field.Update();
    $list.Update();
     
    #Similarly for Impact and Propority..
     
    $Web.Dispose(); 
 Ref: http://howtosharepoint.blogspot.com/2010/11/hide-columns-in-sharepoint-new-edit-and.html

Using Jquery: Hide/Show Columns in SharePoint

This is another post in Paul Galvin's on-going series on how to use jQuery with SharePoint. If you want to learn more about jQuery, I highly recommend: jQuery in Actionhttp://www.assoc-amazon.com/e/ir?t=httppaulgalvi-20&l=ur2&o=1 by Bear Bibeault and Yehuda Katz.
UPDATE (already!): I did think of a better way to locate the <TR> tag I want to hide and wrote about it here.  You may still find this article interesting anyway so I'm leavnig it up.
I want to hide a text field, �Hide Me!� as shown:
Quick and Easy Hide a Text Field
The following jQuery does the trick for me:
1<script type="text/javascript">
2$(function() {
3$('input[title=Hide Me!]').parent().parent().parent().hide();
4});
5</script>
The code is saying, �find me all input fields whose title = Hide Me!.  Then, get its parent and then next parent and the *next* parent (phew!) and invoke the hide() method on that thing, whatever it happens to be.
I figured out that parent structure by viewing the HTML for the form that SharePoint created as shown:
01    <TR>
02    <TD nowrap="true" valign="top" width="190px" class="ms-formlabel">
03    <H3 class="ms-standardheader">
04    <nobr>Hide Me!</nobr>
05    </H3>
06    </TD>
07    <TD valign="top" class="ms-formbody" width="400px">
08      <!-- FieldName="Hide Me!"
09      FieldInternalName="Hide_x0020_Me_x0021_"
10      FieldType="SPFieldText"
11      -->
12      <span dir="none">
13      <input
14      name="ctl00$m$g_bdb23c2c_fde7_495f_8676_69714a308d8e$ctl00$ctl04$ctl02$ctl00$ctl00$ctl04$ctl00$ctl00$TextField"
15      type="text"
16      maxlength="255"
17      id="ctl00_m_g_bdb23c2c_fde7_495f_8676_69714a308d8e_ctl00_ctl04_ctl02_ctl00_ctl00_ctl04_ctl00_ctl00_TextField"
18      title="Hide Me!"
19      class="ms-long" />
20      <br>
21      </span>
22      </td>
23</tr>
This picture shows the same, but marked up with the parents:
Quick and Easy Hide a Text Field
The first parent (1) is a span tag.  Span�s parent (2) is a TD tag and then finally we get to the real parent I want to hide (3) which is the TR tag itself.

Ref: https://www.nothingbutsharepoint.com/sites/eusp/Pages/quick-and-easy-use-jquery-to-hide-a-text-field-on-a-sharepoint-form.aspx

Tuesday, July 17, 2012

Get value from custom fields in workflow task


 To get value from specific field of an item, you need to know the guid of that field. Following is a way to get value from custom workflow task field.



Ex.

Guid testStatusFieldId = workflowProperties.TaskList.Fields["Test Status"].Id;
var status = (string)(Q0AfterProperty.ExtendedProperties[testStatusFieldId]);
if (status == "Q1")
{
                _isQ1 = true;
}
else if (status == "Q4")
{
                _isQ4 = true;
}

Monday, July 9, 2012

Delete all related tasks when workflow complete in sharepoint

When your workflow finished or terminated, you might see some tasks still remain in the task list. Do you want to get rid of them?

Following code help you to remove all tasks remain after workflow complete.


using (var spweb = workflowProperties.Web)
                {
                    spweb.AllowUnsafeUpdates = true;
                    var list = workflowProperties.Item.Tasks.Count;
                    while (list>0)
                    {
                        workflowProperties.Item.Tasks[0].Delete();
                        list = workflowProperties.Item.Tasks.Count;
                    }
                    spweb.AllowUnsafeUpdates = false;
                }

Tuesday, June 26, 2012

Delete items from sharepoint list

In some case, you may want to delete item in sharepoint list. It is easy job.

Ex. You have a list named "Sample List"
using (SPSite siteCollection = new SPSite("http://server")) {
    using (SPWeb site = siteCollection.OpenWeb()) {
        SPList list = site.Lists["Sample List"];

        foreach (SPListItem item in list.Items) {
            item.Delete();
        }
    }
}

Weird dialog box in membership change password control

You may experience with a weird dialog box which ask you to select a user if you want to change password. This happen with mostly Firefox browser (It can be happened with other browser).

The problem is that you use remember password in the Firefox browser. I think it is the bug from Firefox itself. Nothing to do with Asp.net member ship.

To fix this problem, you go to
1- Tool
2- Option
3- Security
4- Remove All

Read item from sharepoint list

In some case, you may want to get all list item in sharepoint list. It is easy to get all those list. below are code for reading list item from sharepoint list.

Ex. You have a list named "Sample List"
using (SPSite siteCollection = new SPSite("http://server")) {
    using (SPWeb site = siteCollection.OpenWeb()) {
        SPList list = site.Lists["Sample List"];

        foreach (SPListItem item in list.Items) {
            //do what you want to do with it.
        }
    }
}
  

Fix error when create task in loop

If you create task in loop, you will got the message "Error occurred".
The problem is that you don't understand the correlation of the task.

The following is how to fix it.