Tag Info

Hot answers tagged

20

new SPFieldLookupValue(item["FieldName"] as String).LookupValue you probably want to check if item["FieldName"] is not null or empty before doing this. SPFieldUserValue works similarly, but you need to pass in an SPWeb into the constructor, which you can retrieve from your SPListItem using SPListItem.ParentList.ParentWeb


17

This is related how instance of SPListItem is being created. Actually in both cases new item is added to the collection of items in list, in first case new item is added explicitly, in the second one implicitly. The point is, that for initializing collection of items in list is used the same method GetItems but with different queries. ...


13

You can use one of the SPUtility.ParseDate overloads. Check the article Converting Date and Time Values for more details.


13

Instead of parsing them manually, try using the SPFieldLookupValue class instead: MSDN: SPFieldLookupValue or the SPFieldMultiChoiceValue class: MSDN: SPFieldMultiChoiceValue


12

You can also use SPQuery and a CAML query, like below. I also agree with what Dave Wise said about PortalSiteMapProvider class. SPQuery query = new SPQuery(); query.Query = "<Where><Eq><FieldRef Name=\"Title\" /><Value Type=\"Text\">YourTitle</Value></Eq></Where>"; query.RowLimit = 1; query.ViewFields = ""; ...


11

A simple solution is to just validate the fields on submit by adding a validation rule in the list settings. Go to List Settings Open Validation settings under the General Settings area In the formula add your formula, I think it will be like this in your scenario: ...


10

You load multiple items. A conflict occurs. Collect all items you need and call "load". After you call load for all items, then run context.executeQueryAsync. Save your selected items as a global variable (this.items): function showTitles(urlColumnName) { var context = new SP.ClientContext.get_current(); var web = context.get_web(); var lists = ...


9

A better way of doing this is the SPFieldLookupValue class. MSDN has a good example. In short, something like: SPListItem lookedUpItem = GetItemSomeHow(); newItem[lookupField] = new SPFieldLookupValue( lookedUpItem.ID, lookedUpItem.Title); newItem.Update();


9

Here is the solution: [Microsoft.SharePoint.SPUser]$spuser = $spWeb.EnsureUser("domain\username") $spListItem[$newSMTName] = $spuser $spListItem.Update()


9

If you use linq to object, you always retrieve ALL records from DB. It is not good if you have got a lot of data. To reduce records you can use CAML query: SPQuery query = new SPQuery(); query.Query = "<Where>" + "<Eq>" + "<FieldRef Name=\"FileLeafRef\"/>" + ...


8

What you are doing is something like this: ItemUpdating (attachItem.Update()) [internal]SPListItem.Update() Which causes the internal Update to fail because you have already done it. Try putting your code in ItemUpdated, rather than ItemUpdating. The flow is then ItemUpdating [internal]SPListItem.Update() ItemUpdated (attachItem.Update())


8

The main point here is that you can work with SharePoint Object Model from PS. So, your task is just to find out the way to change default field value using SharePoint Object Model, and translate it to PS notation. The default value for a column in SharePoint object model is set using SPField.DefaultValue property. So, let's try use this knowledge in PS. I ...


7

If fetching of item["ID"] instead of 'Title' works AND If you are using the SPQuery object. Then, include the columns that you want to fetch in the ViewFields property of SPQuery. This will resolve your issue. Reference: ...


6

I anticipate that your root list has the same event receiver assigned to it, and that causes the recursion. The problem, of course, may be not so obvious, but anyway, to prevent event receiver to fire several times, there is a common practice in SharePoint: just use DisableEventFiring and EnableEventFiring methods. internal void ...


6

The PortalSiteMapProvider is the fastest way to access content and you should be able to do what you need with that.


6

private IEnumerable<string> GetAttachmentUrls(SPListItem item) { return from string fileName in item.Attachments orderby fileName select SPUrlUtility.CombineUrl(item.Attachments.UrlPrefix, fileName); }


5

Use the ID of the lookup target: using(SPSite site = new SPSite("http://test.dev.com")) using (SPWeb web = site.OpenWeb()) { SPList list = web.Lists["Suppliers"]; SPListItem item = list.AddItem(); item[SPBuiltInFieldId.Title] = "Test"; item["Product"] = 1; // Use the ID; get it from ...


5

Is there data in the field that you are calling .ToString on? It sounds like maybe the field is null.


5

Each item has a created date and modified date which you can get through the indexer. item["Created"];


5

You can use the SPWeb GetFile method to retrive the SPFile located at a specified url. Try something like this: SPFile file = SPContext.Current.Web.GetFile(string.Format("{0}/{1}", x, y));


4

The other answers work, but you may want to consider: item[SPBuiltInFieldId.Created]; I'm never sure what happens to the default field names in non-English language setups.


4

The item["Created"] field will store the date and time that item was created. Cast it to a DateTime type to do your comparison.


4

I've had success by not using .ID, but using InternalName with .ContainsFieldWithStaticName(). I also check Hidden and CanBeDeleted on the field as these are more system fields and not custom ones, so I'm not worried about them. Sample below SPWeb web = SPContext.Current.Web; foreach (SPList list in web.Lists) { foreach (SPListItem item in list.Items) ...


4

Did you try the solution mentioned over on this post? Activating and deactivating publishing feature breaks SP2010 wikis? I don't know if it will work for you, but it might be worth a try. I didn't have any success with that previous answer though and went the other way and delved into the bowels of SharePoint and found that even though I was ...


4

Assuming that you already have a reference to the list item (called "item" in the following code sample), you must first get a reference to the list field and then use the GetFieldValue method to read the actual value. SPFieldUser userField= (SPFieldUser)item.ParentList.Fields.GetFieldByInternalName(internalName); var fieldValue = ...


4

Try this SPFieldUser assignedto = mySourceList.Fields["AssignedTo"] as SPFieldUser; ... ... foreach(SPListItem ...) { if(!UserDetails...) { SPFieldUserValue assignedUser = assignedto.GetFieldValue(mySourceListItem["AssignedTo"].ToString()); if(assignedUser != null) { string name = ...


4

2;#post1 is a SPFieldLookupValue where the LookupValue is "post 4" and the LookupId is 2. The LookupId of the item being looked up is the list item ID in the list it's being stored in. To set the value of a lookup field you have a few options: newItem["PostTitle"] = 2; // Just pass the lookup ID newItem["PostTitle"] = new SPFieldLookupValue("2;#post1"); ...


4

You need to use the $expand option in your query string. siteUrl + "/_api/lists/getbytitle('" + listName + "')/items?$select=NameOfLookup/FieldYouWant&$expand=NameOfLookup/NameOfFieldYouWant" So yours would be: "/_api/lists/getbytitle('" + listName + "')/items?$select=$expand=status_x0028_test_x0029/Title&$expand=status_x0028_test_x0029/Title ...


3

UPDATE When using SPListItem from a SPQuery, to access .File properties ensure you have fully loaded the item item = SPList.GetItemById(item.Id) ORIGINAL (Wrong) CheckOutType is an enumeration (value type) so can't be null. http://msdn.microsoft.com/en-us/library/s1ax56ch%28VS.80%29.aspx My guess is that item.File is null Try using: SPFile file = ...


3

Assuming the field type of "validfrom" is DateTime then there should be no need to convert the value, only cast it. You only need to convert/parse if your field type is text, in which case you may consider switching your type to a DateTime or use Marek's advice for conversion. DateTime validFrom = (DateTime) item["validfrom"];



Only top voted, non community-wiki answers of a minimum length are eligible