Archive

Posts Tagged ‘SharePoint 2010’

Programmatically setting a field to hidden within a Content Type #SharePoint #SP2010 #in

February 9, 2011 2 comments

At a client recently, my development colleague was struggling to find out how to set a field attached to a custom Content Type in a list to be hidden to aid in document management (so it is not shown in the Document Properties panel in Microsoft Word).

The following post seemed to suggest an unofficial way:

However, we try to keep things in a supported model if possible.

Through further looking he came across the following property:

    After further playing in code, he managed to crack it with the “.hidden” method.
    Code Example:

This code will set the Hidden flag (same as you can through the UI) against the document library. The above iterates through a List of strings which contains the fields i want to hide.

    1 SPContentType docCT = docLib.ContentTypes[0]; 2 3 foreach (string fieldDispName in fieldsToHide) 4 { 5 SPField field = docCT.Fields[fieldDispName]; 6 docCT.FieldLinks[field.Id].Hidden = true; 7 } 8 9 docCT.Update(); 10

NB: Please not that this works because we are using our own content type attached to the library.

Creating Test / Filler / Example text from Microsoft Word #SharePoint #SP2010 #in ‘#MSOffice

February 7, 2011 1 comment

I always forget the following link when I want some example text in my projects for Plain / Rich Text fields (in Lists and InfoPath Forms), Word Documents (Document Management etc.)

Microsoft Office Help Link: http://blogs.office.com/b/microsoft-word/archive/2009/05/12/quick-tip-filler-text.aspx

Quick Reference

Within Word type the following:

  • =rand()
  • =lorem()

These produce a single paragraph, but the function is configurable via the following parameters:

  • =rand(insert the number of paragraphs, insert the number of sentences)
  • =lorem(insert the number of paragraphs, insert the number of sentences)

Example: =lorem(1,6)

A reference / links to Creating Custom Actions in #SharePoint #SP2010 #in

February 7, 2011 Leave a comment

Useful Information: Determining if the web service return is a File or a Folder in GetListItems (Lists.asmx) #in #SP2010 #SharePoint

February 7, 2011 Leave a comment

Just a useful quick tip I noticed whilst answering a question for a client the other day.

In the return of the GetListItems method of the Lists.asmx Web Service, one of the fields is:

  • ows_FSObjType

This essentially allows us to determine of the return is either a File or a Folder.

The detail of the return is as follows:

  • <Item ID>;#<File System Object Type>
  • Example Data: 1;#0

Options for the File System Object Type:

  • 0 = File
  • 1 = Folder

Useful JavaScript Function: PreSaveAction #SP2010 #SharePoint #PS2010 #ProjectServer #in

January 25, 2011 1 comment

Just a quick blog post to talk about a standard JavaScript function that exists in SharePoint 2007 and 2010 – PreSaveAction().

Now I have done extra validation and functions on list forms in allsorts of ways before (some of which is blogged on this site) and it is only recently that this standard function has been brought to my attention. (Thanks @GlynClough via twitter).

Once I learned about this function I of course google’d about it to see what I could find and came across the following blog articles:

These are excellent articles that explain the function, however in the way I tend to work in my projects it posed a problem.

We are essentially overriding a standard SharePoint JavaScript function and the articles assume that you can place a Content Editor Web Part onto the page to add the functionality.

Now in SharePoint 2010 this is less of a problem, since there are very good and official ways to add web parts to list form pages, even in a Publishing Site.

However, in Microsoft Office SharePoint Server 2007 utilising publishing sites, editing newform.aspx and editform.aspx pages caused many issues.

This forced the consultant / developer to create their own new / edit forms and pages in order to insert sometimes a very small amount of code.

Since most of my projects seem to involve the Publishing feature in one way or another, I typically place JavaScript reference files in the Master Page.  This means that my JavaScript code is running on every page.

So, getting to the point of this article, I have modified the general example that is supplied on other sites to take into account this method of JavaScript development by simply querying what page I am on before I run my validation tests and functions.

Code Example:

1 <script type='text/javascript'> 2 3 //global variable to be used in multiple functions 4 var currentPathname = location.pathname; 5 //set to lowercase to make comparisons in the code easier 6 currentPathname = currentPathname.toLowerCase(); 7 8 function PreSaveAction() {; 9 10 if (currentPathname.indexOf('Enter Pathname here IE. /lists/tasks/editform.aspx') != -1) { 11 //Setup your validation checks here 12 if (1 == 1) { 13 alert('Validation passed, let SharePoint continue'); 14 return true; 15 } 16 else { 17 alert('Validation failed, stop SharePoint from saving the item'); 18 return false; 19 } 20 } 21 22 //If we have got to this point, then no pages need pre-validation. Let SharePoint continue. 23 return true; // OK to proceed with the save item 24 } 25 26 </script>

Splitting a date into Year, Quarter and Month for analysis purposes in InfoPath 2010 #SP2010 #SharePoint #in

January 25, 2011 1 comment

Last week at a client I had the requirement to split a date into various parts for various uses on the form and the list for analysis purposes.

Although not a lot of hard work (it was done and dusted within 15 minutes).  I thought I would share some of my findings and the formulas and rules I used to achieve my requirement.

The Starting Point – Fields and Settings

Initially we need some fields to store the data:

Column Name Column Type Column Settings Comments
Activity Date Date and Time Date Only All other fields will be derived from this column
Year Single Line Of Text No. Characters: 4 Stored as text
Quarter Number No. Decimals: 0
Min: 1
Max: 4
 
Month Single Line Of Text No. Characters: 9 For storing Month names.

Example:
– January
– September
– December

Month No. Number No. Decimals: 0
Min: 1
Max: 12
 
Month No. Text Single Line Of Text No. Characters: 2 Value Example:
– 01
– 09
– 12

To aid with sorting when combining with Year values.

Example (YYYY MM):
– 2011 01
– 2011 09
– 2011 12

Formula / Default Values:

Column Name Formula / Default Values Comments
Year

substring(../my:Activity_x0020_Date, 1, 4)

SharePoint stores dates in the following format to avoid regional issues:

– YYYY-MM-DDTHH-MM-SSZ

Example:

– 2011-01-24T22-25-00Z

The formula to the left takes this into account.

Month No. substring(../my:Activity_x0020_Date, 6, 2) As a number field, this will automatically take off the preceding zero.
Month No. Text substring(../my:Activity_x0020_Date, 6, 2) As a text field, the preceding zero will be retained.

NB: By copying and pasting the default values above, it is likely that you will need to remap the field selector(../my:Activity_x0020_Date) to accommodate the date field in your form.

Rules:

Column Name Condition Action Run Remaining Rules
Month No. Month No. >= 10 Set Quarter = 4 No
  Month No. >= 7 Set Quarter = 3 No
  Month No. >= 4 Set Quarter = 2 No
  Month No. >= 1 Set Quarter = 1 No
Month No. Text Month No. Text = “01” Set Month = “January” No
  Month No. Text = “02” Set Month = “February” No
  Month No. Text = “03” Set Month = “March” No
  Month No. Text = “04” Set Month = “April” No
  Month No. Text = “05” Set Month = “May” No
  Month No. Text = “06” Set Month = “June” No
  Month No. Text = “07” Set Month = “July” No
  Month No. Text = “08” Set Month = “August” No
  Month No. Text = “09” Set Month = “September” No
  Month No. Text = “10” Set Month = “October” No
  Month No. Text = “11” Set Month = “November” No
  Month No. Text = “12” Set Month = “December” No

The Result:

Categories: Work Tags: ,

Using #jQuery to attach regular expression validation to a #SharePoint list form field #SP2010 #PS2010 #ProjectServer #in

January 19, 2011 4 comments

On one of my current projects I have had cause to ensure that no special characters are in the Title field of a SharePoint 2010 list form.

This is because we are using the Title field as the Plan Name in a Project Create process in Project Server 2010.  As a result we need to ensure that the Event Handler we are creating has validated data that the PS2010 PSI Web Service can accept for the Plan Name.

I created the following script to validate the data being entered into the Title field of the list form as it is being entered.

I manipulate the Save Buttons in the ribbon and the list form to ensure that the user cannot enter invalid data into the form.

To ensure a good user experience, I add an extra <DIV> element next to the Title field to notify the user of what is going on.

Example Code:

1 <script type='type/javascript'> 2 3 _spBodyOnLoadFunctionNames.push("attachToTitleField"); 4 5 function attachToTitleField() { 6 var titleField = $('input[title=Title]'); //Find title field on the form 7 8 //Add a DIV element to provide a place for an error message from the RegEx validation 9 titleField.parent().append("<div id='GilesH_titleValidation'></div>"); 10 11 //Use jQuery to attach the validateAlphanumeric function on keyup 12 titleField.keyup(function() { 13 validateAlphanumeric(titleField.val()); 14 }); 15 } 16 17 function validateAlphanumeric(stringValue) { 18 19 //Allows alphanumeric only for the 1st character 20 //after the 2nd character, alphanumeric and spaces allowed upto 255 characters 21 var regExpressionValue = /^[0-9A-Za-z][0-9A-Za-z\s]{0,255}$/; 22 var re = new RegExp(regExpressionValue); 23 24 if (stringValue.match(re)) { 25 //alert("Successful match"); //Debug 26 27 //Allow Save 28 $('a[id=Ribbon.ListForm.Edit.Commit.Publish-Large]').attr("style", "display:inline-block;"); //Show Ribbon Save Button 29 $('input[id*=SaveItem]').attr("disabled",""); //Re-enable Form Save Button 30 31 //Blank Error Message 32 $('div[id*=GilesH_titleValidation]').text(""); //Blank custom error message 33 } else { 34 //alert("No match"); //Debug 35 36 //Stop Save 37 $('a[id*=Ribbon.ListForm.Edit.Commit.Publish-Large]').attr("style", "display:none;"); //Hide Ribbon Save Button 38 $('input[id*=SaveItem]').attr("disabled","true"); //Disable Form Save Button 39 40 //Provide Error Message 41 $('div[id*=GilesH_titleValidation]').text("The title you have entered is invalid. Only alphanumeric characters and spaces are allowed."); 42 } 43 } 44 45 </script> 46 47

Found a useful post about #SharePoint Web Services #SP2010 #in

January 12, 2011 Leave a comment

In my travels of playing with the SharePoint Web Services, I found the following post useful.

Although it is based on WSS 3.0 / MOSS 2007.  Much of it is still relevant for SharePoint 2010.

http://bhavtosh.wordpress.com/2009/10/05/sharepoint-2007-web-services/

Using jQuery to return all members / users of a group #in #SP2010 #SharePoint

January 12, 2011 1 comment

I’ve recently had cause to determine if a user is within a particular group in SharePoint and act upon the output.

Luckily the standard SharePoint web services allow to query for this information utilising the User Group service at location: http://<server>/_vti_bin/usergroup.asmx.

In particular the method GetUserCollectionFromGroup will provide the complete list including the Windows SAMAccountName (login name) as part of the return.

However, in this instance I was caught a little by a permissions problem.  Although I could query the web service, it kept returning a status of “parsererror” and an output of “Access Denied”.

This was resolved by changing the group settings to allow “Everyone” to read the membership of the group.

GroupSettingsBlog

Code Example

The following code utilises jQuery to access the web service:

1 <script type='text/javascript'> 2 3 _spBodyOnLoadFunctionNames.push("getUserCollectionFromGroup"); 4 5 function getUserCollectionFromGroup() { 6 var soapEnv = 7 "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'> \ 8 <soap:Body> \ 9 <GetUserCollectionFromGroup xmlns='http://schemas.microsoft.com/sharepoint/soap/directory/'> \ 10 <groupName>Group Name Here</groupName> \ 11 </GetUserCollectionFromGroup> \ 12 </soap:Body> \ 13 </soap:Envelope>"; 14 $.ajax({ 15 url: "/_vti_bin/usergroup.asmx", 16 type: "POST", 17 dataType: "xml", 18 data: soapEnv, 19 complete: getUserCollectionFromGroupReturn, 20 contentType: "text/xml; charset=\"utf-8\"" 21 }); 22 } 23 function getUserCollectionFromGroupReturn(xData, status) { 24 alert("Status: " + status); 25 alert("Output: " + xData.responseText); 26 } 27 </script> 28

Determining delegation mode and user via jQuery #in #ProjectServer #PS2010 #SP2010

January 12, 2011 Leave a comment

In my current project I have recently had the requirement to determine whether Project Server 2010 was in delegation mode.

If the user was in delegation mode, we then passed the unique Display Name of the delegation user and passed it to a web service to return more data about the resource.

DelegateScreenshot

The information bar in PS2010 lets us know that we are in delegation mode and in jQuery we can interrogate this to get the information we need.

The following code uses jQuery to determine if we are in delegation mode and sets the Display Name and isDelegate variables accordingly:

1 <script type='text/javascript'> 2 var delegateName = ""; 3 var isDelegate = 0; 4 5 _spBodyOnLoadFunctionNames.push("checkDelegateMode"); 6 7 function checkDelegateMode() { 8 //Am I in delegate mode? Check the SP2010 Information bar for the word delegate 9 $('span[id=status_1_body]').each(function() { 10 11 var textEval = $(this).text(); 12 13 if (textEval.indexOf('delegate') != -1) { 14 //The word delegate has been found - lets find the delegate Resource Display Name 15 16 var strongCount = 0; //The delegate is the first item surrounded in the <STRONG> tag 17 18 $(this).find("strong").each(function() { 19 20 if (strongCount == 0) { 21 delegateName = $(this).text(); //Store the display name 22 isDelegate = 1; //Set that the delegate has been found 23 24 } 25 26 strongCount = strongCount + 1; 27 28 }); 29 } 30 31 }); 32 } 33 </script>

Design a site like this with WordPress.com
Get started