Posts

Showing posts from November, 2009

Create and Modify User Profiles in the User Profile Store of SharePoint

This example creates a user profile from the master connection configured on the server. It displays the message 'User profile created' after a successful execution. //Creates a user profile. Obtains the property values from the default //domain controller or the master connection that is configured on the //server using System; using System.Collections.Generic; using System.Text; using Microsoft.SharePoint.Administration; using Microsoft.Office.Server.UserProfiles; using Microsoft.SharePoint; using Microsoft.Office.Server; using System.Web; namespace UserProfilesConsoleApp { class Program { static void Main(string[] args) { try { using (SPSite site = new SPSite("http://servername")) { ServerContext context = ServerContext.GetContext(site); UserProfileManager profileManager = new UserProfileManager(context); string sAccount = "domainname\\username"; if (!profileManager.UserExists(sAccount)) { UserProfile profile = profileMana...

Programmatically creating views for a SharePoint discussion forum

Image
As you can see I'm still working on sites, lists and views and I ran into another challenge. It started out very basic. I wanted to create a discussion forum list on a site. // Create new list listGuid = newWeb.Lists.Add("Forum", "ForumDescription", "Forum", "00BFEA71-6A49-43FA-B535-D15C05500108", 108, "", SPListTemplate.QuickLaunchOptions.Off); SPList forumList = newWeb.Lists[listGuid]; After succesfully creating the forum list I wanted to add a custom view to the list. That seemed to be easy enough as well. // Add an extra view to the list viewFields = new StringCollection(); viewFields.Add("LinkDiscussionTitle"); viewFields.Add("Author"); viewFields.Add("DiscussionLastUpdated"); newView = forumList.Views.Add("Portal View", viewFields, "", 100, true, false); newView.Update(); Unfortunately creating a view using the code above causes the link on the title takes yo...

Add hidden metadata field to SPListItem

Image
You need to add a new field to the list that is marked Hidden="TRUE". You can do this via the object model: using (SPSite sps = new SPSite("http://someServer/")) { using (SPWeb spw = sps.OpenWeb()) { SPList spl = spw.Lists["Shared Documents"]; String strNewField = " "; try { spl.Fields.AddFieldAsXml(strNewField); } catch (SPException spe) { // Notify error that a duplicate name was detected } spl.Update(); } } Note : These hidden field will not appear when we try to create custom view using site action.

Setting Permissions Through Code

When working with permissions through the SharePoint API, there are some key objects to take note of. SPUser – A actual security object in SharePoint (AD User, AD Group or Forms Based User or Group). SPGroup – A SharePoint group defined through SharePoint. SPRoleDefinition – A actual role (permission level) defined in SharePoint (Read, Full Control etc…) SPRoleAssignment – The actual assignment between a SPUser/SPGroup and a SPRoleDefinition // ******************** Adding a SPUser to a SharePoint Group ******************** // Create the site that contains our list SPSite oSite = new SPSite("< >"); // Open the web object SPWeb oWeb = spSite.OpenWeb(); // Get the group that we want to add the user to SPGroup oGroup = oWeb.Groups["< >"]; // Get the user that we want to add to the group SPUser oUser = oWeb.AllUsers["domainlogin"]; // Now we add the user to the groups user collection oGroup.AddUser(oUser); // Update the group...

Programmatically create a view (custom view) of a list

code snippet for creating a custom view ===================================== SPSite oSite = new SPSite([Site URL]);// [Site URL] change it to your sharepoint site URL SPWeb oWeb = oSite.OpenWeb(); SPList oList = oWeb.Lists["shared documents"]; SPViewCollection oViewCollection = oList.Views; string strViewName = "MyCustomView"; System.Collections.Specialized.StringCollection viewFields = new System.Collections.Specialized.StringCollection(); viewFields.Add("Name"); viewFields.Add("Type"); string query = " " + " mysample ";// here you can filter your items using the selected item in the dropdownlist oViewCollection.Add(strViewName, viewFields, query, 100, true, false); oWeb.Update(); Now refresh the list in UI and then you can see the “MyCustomView” in the selection list of views.

Using the SharePoint ‘Person or Group’ field in code

The following code is a simple example of a Console Application which accepts a semi-colon delimited list of aliases (i.e. domain\user;domain\user;domain\user) and then adds them to a ‘Person or Group’ field called ‘MyPersonField’ in a list. Console.WriteLine("Enter a ; delimited list of domain\alias that need to be added:"); string sAliases = Console.ReadLine(); //captures whatever the user entered string sValueToAddToFieldInSP = ""; //used to build the full string needed for the person field string sAllContacts = ""; using (SPSite site = new SPSite(“http://sites/site/yoursite”)) { site.AllowUnsafeUpdates = true; using (SPWeb web = site.RootWeb) { web.AllowUnsafeUpdates = true; string[] aAliases = sAliases.Split(';'); foreach (string sAlias in aAliases) { SPUser user = web.EnsureUser(sAlias); sAllContacts += user.ID.ToString() + ";#" + user.LoginName.ToString() + ";#"; } web.Update(); } ...