Sunday, 22 February 2015

SharePoint 2010 Query to get List items assigned to current logged in user with Status

 Retrieve all list items assigned to current logged in user based on Status value


SPUser currentUser = SPContext.Current.Web.CurrentUser;

string name = currentUser.Name.ToString();

SPList list = web.Lists["Listname"];

SPQuery dataQuery= new SPQuery();

dataQuery.ViewFields = "<FieldRef Name='ID'/>";

dataQuery.Query = "<Where><And><Gt><FieldRef Name='ID' /><Value Type='Counter'>0</Value></Gt><And><Eq>"
+ "<FieldRef Name='UserName' /><Value Type='User'>"+name +"</Value></Eq><Or><Eq>"
+ "<FieldRef Name='Status' /><Value Type='Choice'>Status 1</Value></Eq><Or><Eq>"
+ "<FieldRef Name='Status' /><Value Type='Choice'>Status 2</Value></Eq><Eq>"
+ "<FieldRef Name='Status' /><Value Type='Choice'>Status 3</Value></Eq></Or></Or></And></And></Where>";



Cheers :)
 

SharePoint 2010 Programatically Upload document and update column of document library

In this post i am sharing learning about "Upload a document  through File Upload control to a SharePoint document library and update a document library too", Below is the code

private void UploadFile(string fileName, byte[] fileBytes)
        {
                using (SPSite site = SPContext.Current.Site)
                {
                    if (site != null)
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPFolder docLibrary = web.Folders["DocumentLibraryName"];
                            if (docLibrary != null)
                            {
                                Boolean replaceExistingFiles = true;
                               
                                SPFile spFile = docLibrary.Files.Add(fileName, fileBytes, replaceExistingFiles);
                                spFile.Item["ColumnName"] = "Some information";
                               
                                spFile.Item.Update();
                                docLibrary.Update();
                            }
                        }
                    }
                }
            }

Cheers :)

Saturday, 16 November 2013

SharePoint 2010 update current Logged in user Name in Modified By field of Custom List code running "SHAREPOINT\system"


Recently i worked on requirement in which user has only READ access on SharePoint site but he can submit input to SharePoint list through custom form

To manage the access permission for the user who has only read access on site, we have two approaches

1. Use runwithelevatedprivileges
2. Use user token

i used user token approach

Before updating the Author field in the list  used below code

protected void Button1_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                SPUser systemUser = SPContext.Current.Web.AllUsers[@"SHAREPOINT\system"];
                SPUser currentUser = SPContext.Current.Web.CurrentUser;
                using(SPSite site=new SPSite(SPContext.Current.Web.Url,systemUser.UserToken))
                {
                    site.AllowUnsafeUpdates = true;
                    using (SPWeb web = site.OpenWeb())
                    {
                        web.AllowUnsafeUpdates = true;
                        try
                        {
                            SPList list = web.Lists["List Name"];
                            SPListItem listItem = list.Items.Add();
                         
   string userName = currentUser.ID + ";#" + currentUser.Name;
                            listItem["Author"] = userName; //Modified By field.
                            listItem.Update();
                            list.Update();          
                        }
                        catch (Exception ex)
                        {
           // Exception handling logic
                        }
                    }
                }
            }        
        }

cheers!!

Monday, 16 July 2012

JAVA SCRIPT: SharePoint Get Current User



JAVA SCRIPT: SharePoint Get Current User 


Use following Java Script

<script language="javascript" type="text/javascript">
_spBodyOnLoadFunctionNames.push("GetCurrentUserName");
function GetCurrentUserName()
{
var Loginname = document.getElementById("zz8_Menu").innerHTML ;
var end = Loginname.indexOf("<");
var nameOnly = Loginname.substring(8, end);
alert(nameOnly);
}
</script>

Cheers :)

JQUERY: SharePoint 2010 Get Current User for web application


JQUERY: SharePoint 2010 Get Current User for web application

·         Download the latest jquery.js from SPServices Jquery.
·         Most releases of the library include both a minified and a normal version of the release. If you would like to understand the workings of the library, look at the normal version, but use the minified version for any production use.
·         Upload the minnified js file to the style library of your site.

The library can be implemented by adding a reference to it into a single page, a page layout, a content query web part or a master page, depending upon your desired scope of use

I recommend storing the jQuery library and SPServices in a Document Library in your Site Collection and referencing it as needed, like this:

<script language="javascript" type="text/javascript" src="/jQueryLibrary/ jquery.SPServices-0.7.1a.min.js"></script>

<script language="javascript" type="text/javascript" src="/jQueryLibrary/ jquery.SPServices-0.7.1a.min.js"></script>

Obviously, the src attributes should point to wherever you've put the .js files.

·         Add the following code
$().SPServices.SPGetCurrentUser({ fieldName: "Name", debug: false });

This is a quick script to check if everything is working OK:
<script type="text/javascript">
alert ("js on.");
$(document).ready(function() {
alert ('jQuery on.');
var theuser= $().SPServices.SPGetCurrentUser();
alert ('Current user for web application is : ' + theuser);
});
</script>

Cheers :)

Sunday, 15 July 2012

Basic PowerShell Commands


·       Get-Help


The first PowerShell cmdlet should learn is Get-Help. You can use this command to get help with any other command.
For example, if you want to know how the Get-Process command works, you can type:
Get-Help -Name Get-Process
And Windows will display the full command syntax.
You can also use Get-Help with individual nouns and verbs.
For example, to find out all the commands you can use with the Get verb, type:

Get-Help -Name Get-*

·       Get-Service

The Get-Service command provides a list of all of the services that are installed on the system. If you are interested in a specific service you can append the -Name switch and the name of the service (wildcards are permitted) When you do, Windows will show you the service’s state.

·       ConvertTo-HTML

PowerShell can provide a wealth of information about the system, but sometimes you need to do more than just view the information onscreen. Sometimes, it’s helpful to create a report you can send to someone. One way of accomplishing this is by using the ConvertTo-HTML command.
To use this command, simply pipe the output from another command into the ConvertTo-HTML command. You will have to use the -Property switch to control which output properties are included in the HTML file and you will have to provide a filename.
You want to create an HTML report that lists the name of each service along with its status (regardless of whether the service is running). To do so, you could use the following command:
Get-Service | ConvertTo-HTML -Property Name, Status > C:\services.htm

·       Export-CSV

Just as you can create an HTML report based on PowerShell data, you can also export data from PowerShell into a CSV file that you can open using Microsoft Excel. The syntax is similar to that of converting a command’s output to HTML. At a minimum, you must provide an output filename. For example, to export the list of system services to a CSV file, you could use the following command:
Get-Service | Export-CSV c:\services.csv

·       Select-Object

If you tried using the command above, you know that there were numerous properties included in the CSV file. It’s often helpful to narrow things down by including only the properties you are really interested in. This is where the Select-Object command comes into play. 
The Select-Object command allows to specify specific properties for inclusion. For example, to create a CSV file containing the name of each system service and its status, you could use the following command:
Get-Service | Select-Object Name, Status | Export-CSV c:\services.csv

·       Get-Process

Just as you can use the Get-Service command to display a list of all of the system services, you can use the Get-Process command to display a list of all of the processes that are currently running on the system.

·       Stop-Process

Sometimes, a process will HANG. When this happens, you can use the Get-Process command to get the name or the process ID for the process that has stopped responding. You can then terminate the process by using the Stop-Process command. Terminate a process based on its name or on its process ID. For example, Terminate WINWORD by using one of the following commands:
Stop-Process -Name winword
Stop-Process -ID xxxx
Keep in mind that the process ID may change from session to session.
Cheers :)

PowerShell Commands


PowerShell commands to obtain critical system information

·       Determine screen resolution

When a software package requires a certain resolution or minimum resolution, you can query the local computer to return the resolution of the primary monitor. Multiple monitors are not supported in this command.

Get-DisplaySetting

Couple the command with a variable to determine the computer’s name and export it to a local (or central file):

               $NetBIOSName = gc env:computername
               Get-DisplaySetting > C:\$NetBIOSName.txt

·      Enumerate USB devices

Get-USB -Computername System.FullyQualified.Domain
you can run this command against remote systems natively. Running the command with the -computername variable will connect the command to a remote system and return the same results as if it were executing locally.

·   Processor information

Get-processor

Cheers :)