Tuesday, September 7, 2010

Getting a SharePoint Field Value In C# | ARB Security Solutions

Getting a SharePoint Field Value In C# | ARB Security Solutions


public static string GetFieldValue(SPListItem listItem, string fieldName)
{
string text = string.Empty;
if (fieldName == string.Empty)
{
return text;
}

try
{
object myObj = listItem[fieldName];
return ((myObj != null) ? myObj.ToString() : string.Empty);
}
catch
{
return string.Empty;
}
}

public static string GetHyperLinkFieldValue(SPListItem listItem, string fieldName)
{
string text = string.Empty;
if (fieldName == string.Empty)
{
return text;
}

try
{
SPFieldUrlValue value = new SPFieldUrlValue(listItem[fieldName].ToString());
return ((value != null) ? value.Url : string.Empty);
}
catch
{
return string.Empty;
}
}

Sharepoint Tips And Tricks: Code Practices - getting\setting values from\to the lookup and the hyperlink fields

Sharepoint Tips And Tricks: Code Practices - getting\setting values from\to the lookup and the hyperlink fields


Code Practices - getting\setting values from\to the lookup and the hyperlink fields
How to get data from a field that is a lookup? how to set data into a field that is a hyperlink?
I don't believe I didn't write about this yet. It's so common!


For both cases, sharepoint object model exposes classes to help us get or set the data we want. These are the SPFieldLookupValue and the SPFieldUrlValue.


Example 1: Set the url field of a link

Use the SPFieldUrlValue class to create an object that holds the url to link to, and the title to display:
SPList list = web.Lists["Links"];

SPListItem newLink = list.Items.Add();

SPFieldUrlValue value = new SPFieldUrlValue();

value.Description = "test";

value.Url = "http://www.microsoft.com/sharepoint";

newLink["URL"] = value;

newLink.Update();

Example 2: Get the url field of a link

Use the SPFieldUrlValue class to create an object that gets the url and description:

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

SPListItem existingLink = list.Items[0];

SPFieldUrlValue value = new SPFieldUrlValue(existingLink["URL"].ToString());

string linkTitle = value.Description;

string linkURL = value.Url;



Example 3: Set the value of a lookup field for a known title and ID

In the following example I am using SPFieldLookupValue to set the value of a lookup field ("Group Name") to item "Program Operations", whose ID is 14:

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

SPListItem newBranch = list.Items.Add();

newBranch["Title"] = "A New Branch";

SPFieldLookupValue newValue = new SPFieldLookupValue(14,"Program Operations");

newBranch["Group Name"] = newValue;

newBranch.Update();

Example 4: Get the value of a lookup field from an item

Here I am reading the value of the group name field (which is a lookup field in the branches list):
SPList list = web.Lists["Branches"];

SPListItem existingBranch = list.Items[0];

SPFieldLookupValue group = new SPFieldLookupValue(existingBranch["Group Name"].ToString());

int lookedUpItemID = group.LookupId;

string lookedUpItemTitle = group.LookupValue;

Get URL Value of SharePoint Hyperlink Field « Alex's SharePoint Blog

Get URL Value of SharePoint Hyperlink Field « Alex's SharePoint Blog: "I needed to get the value of a SharePoint hyperlink field in a custom list the other day so I simply tried to access the value of the field directly in the object model:
SPListItem item = list.GetItemById(1);
string URL = item[“URL”].ToString();
The only problem with this is whilst it gets the value, you end up with two URL’s separated by a comma. This is because one is the actual display text for the field and the other is the value (the actual URL itself).
In order to get just the URL value, you need to use the following object model code:
SPFieldUrlValue value = new SPFieldUrlValue(item[“URL”].ToString());
string URL = value.Url;"

Write a value into the property bag - Corey Roth - DotNetMafia.com - Tip of the Day

Write a value into the property bag - Corey Roth - DotNetMafia.com - Tip of the Day

I have ran into a few people that didn't even know what the property bag is on the SPWeb object, so I figured that would be a good topic for a post. The property bag exposed by the Properties and AllProperties collections on the SPWeb object gives the developer a way to cache information on a site. It can be compared to ASP.NET's cache object but the values are shared across the farm and will persist through server reboots. Unfortunately, using the property bag isn't as simple as you would think. In fact to get it working in all situations, it requires a number of settings to allow it to work. It's probably enough to scare a lot of developers away from it.

Before we start, let me talk about the differences between Properties and AllProperties. Properties is a collection of type SPPropertyBag. It is sort of considered deprecated. You can use it to add new items to the property bag, but it will only return a subset of everything in the property bag when retrieving data. Why is that? I have no idea. So instead, Microsoft later implemented the AllProperties Hashtable which contains everything in the property bag.

Here is what the start of our code block looks like to write to the property bag. This assumes it is inside a using block to get access to an SPWeb.

Write a value into the property bag - Corey Roth - DotNetMafia.com - Tip of the Day

Write a value into the property bag - Corey Roth - DotNetMafia.com - Tip of the Day

Friday, September 3, 2010

CTX108094 - How to Remove a Bad Web Part from a SharePoint Server - Citrix Knowledge Center

CTX108094 - How to Remove a Bad Web Part from a SharePoint Server - Citrix Knowledge Center: "In such situations, append ?Contents=1 to the Web Part Page’s URL. For example, http://gardenco/default.aspx?contents=1. The Web Part Maintenance Page is displayed and from there you can delete the offending Web Part."

Add-SPShellAdmin

Add-SPShellAdmin

Thursday, September 2, 2010

SharePoint 2010 Products administration by using Windows PowerShell

SharePoint 2010 Products administration by using Windows PowerShell

Accessing Windows PowerShell for SharePoint 2010 Products
After installing SharePoint 2010 Products, the applicable Windows PowerShell cmdlets are available by using the SharePoint 2010 Management Shell, or by using the Windows PowerShell console. With the management shell, you can manage every aspect of SharePoint 2010 Products. You can create new site collections, Web applications, user accounts, service applications, proxies, and more. The commands from the management shell output SharePoint objects based on the Microsoft .NET Platform. These objects can be applied as input to subsequent commands or stored in local variables for later use.

With the management shell, you do not have to register the snap-in that contains the cmdlets. Registration of the Microsoft.SharePoint.PowerShell.dll module for SharePoint 2010 cmdlets is automatic, as a result of the line Add-PSSnapin Microsoft.SharePoint.PowerShell in the SharePoint.ps1 file located in %CommonProgramFiles%\Microsoft Shared\Web Server Extensions\14\Config\PowerShell\Registration. If you choose to use the Windows PowerShell console, you must register this snap-in manually.

Whether you are using the management shell or the Windows PowerShell console, you can also load additional snap-ins. For more information, see Customizing Profiles (http://go.microsoft.com/fwlink/?LinkId=183166).

To access the SharePoint 2010 Management Shell
1.On the Start menu, click All Programs.

2.Click Microsoft SharePoint 2010 Products.

3.Click SharePoint 2010 Management Shell.

Note:
The SharePoint 2010 Management Shell and the Windows PowerShell console also differ in the use of the ReuseThread option, which defines how the threading model is used. The management shell's use is defined by this line, {Host.Runspace.ThreadOptions = "ReuseThread"}, which is in the SharePoint.ps1 file. For more information, see PS Thread Options (http://go.microsoft.com/fwlink/?LinkId=183145).


Permissions
Before you can use the management shell and the Windows PowerShell cmdlets, verify that you meet the following minimum requirements: See Add-SPShellAdmin.

If you do not have membership in the SharePoint_Shell_Access role or WSS_Admin_WPG local group, use the Add-SPShellAdmin cmdlet. When the Add-SPShellAdmin cmdlet is used, the user is added to the WSS_Admin_WPG group in all front-end Web servers and is added to the SharePoint_Shell_Access role. If the target database does not have a SharePoint_Shell_Access role, the role is automatically created. Once the Add-SPShellAdmin cmdlet has been run, the user can run SharePoint 2010 Windows PowerShell cmdlets in a multiple-server farm environment.

When you run the Add-SPShellAdmin cmdlet to add a user to the SharePoint_Shell_Access role, you must have the following security permissions:

Securityadmin server role access on the SQL instance and the db_owner role in a database.


Administrative permission on the local computer.


Note:
Typically, the person that will use the Add-SPShellAdmin cmdlet must be associated with the user account that was used for Setup.


You must run the Add-SPShellAdmin cmdlet for all databases to which you want to grant access. If no database is specified, the farm configuration database is used. If you do specify a database, the farm content database will be included in addition to the farm configuration database you specify.

To see a list of all of the *SPShellAdmin cmdlets, from a Windows PowerShell command prompt, type Get-Command –Noun SPShellAdmin.

Learning Windows PowerShell
There are several Windows PowerShell learning resources for SharePoint IT professionals who are not familiar with Windows PowerShell.

Import a list or document library (SharePoint Server 2010)

Import a list or document library (SharePoint Server 2010): "Import a site, list or document library
You can use Windows PowerShell to manually import a site, list, or document library or as part of a script that can be run at regular intervals.
To import a site, list or document library by using Windows PowerShell
Verify that you meet the following minimum requirements: See Add-SPShellAdmin.
On the Start menu, click All Programs.
Click Microsoft SharePoint 2010 Products.
Click SharePoint 2010 Management Shell.
At the Windows PowerShell command prompt (that is, PS C:\>), type the following command, and then press ENTER:
Import-SPWeb -Identity -Path [-Force] [-NoFileCompression] [-Verbose]"

Get value collection of a SharePoint Choice Field

Get value collection of a SharePoint Choice Field

public static List GetChoiceFieldValues(string listName,string fieldName, string siteCollection, string webSite)
{
List fieldList;

SPSite spSite = null;
SPWeb spWeb = null;

try
{
if (siteCollection != null)
spSite = new SPSite(siteCollection);
else
spSite = SPContext.Current.Site;

if (webSite != null)
spWeb = spSite.OpenWeb(webSite);
else
spWeb = spSite.OpenWeb();

SPList spList = spWeb.Lists[listName];

SPFieldChoice field = (SPFieldChoice)spList.Fields[fieldName];

fieldList = new List();

foreach (string str in field.Choices)
{
fieldList.Add(str);
}
}
catch (Exception ex)
{
LogException(ex);
throw;
}
finally
{
if(spWeb != null)
spWeb.Close();

if(spSite != null)
spSite.Close();
}

return fieldList;
}

Chapter 1: SharePoint 2010 Developer Roadmap

Chapter 1: SharePoint 2010 Developer Roadmap

Learn Windows PowerShell in 21 Minutes
Working with Windows PowerShell is much easier than writing MS-DOS-style batch files. It's easier because the Windows PowerShell scripting language treats everything as an object. You can create and program against .NET objects as well as COM objects. Furthermore, Windows PowerShell has first-rate support for calling out to EXE-based utilities and passing parameters to execute specific commands.

There are two common ways in which you can use Windows PowerShell. First, you can execute commands interactively using the Windows PowerShell console window. Second, you can write scripts to automate administration tasks. Then you can execute these scripts either on demand or through some type of scheduling mechanism.

Let's start by getting familiar with the Windows PowerShell console window. You can launch the Windows PowerShell console window from the following path in the Windows Start menu.

Start\All Programs\Accessories\Windows PowerShell\Windows PowerShell

When the Windows PowerShell console appears, you should type and execute the following three commands interactively.

1.Type cd\ and then press Enter. This sets the current location to the root of the C:\ drive.


2.Type cls and then press Enter. This clears the console window.


3.Type 2 + 2 and then press Enter. This performs a mathematical calculation and displays the result.


If you followed these steps correctly and executed each of the three commands, your console window should look like the one in Figure 1-13.



Figure 1-13: You can execute commands interactively from the Windows PowerShell console window.